{"id":36999,"date":"2018-03-24T03:01:31","date_gmt":"2018-03-23T17:01:31","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=36999"},"modified":"2018-03-24T05:47:01","modified_gmt":"2018-03-23T19:47:01","slug":"javascript-instanceof-food-emoji-guessing-game-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-instanceof-food-emoji-guessing-game-tutorial\/","title":{"rendered":"Javascript instanceof Food Emoji Guessing Game Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/food_oops.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript instanceof Food Emoji Guessing Game Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/food_oops.jpg\" title=\"Javascript instanceof Food Emoji Guessing Game Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript instanceof Food Emoji Guessing Game Tutorial<\/p><\/div>\n<p>There are a few salient points behind today&#8217;s &#8220;Food Emoji Guessing Game&#8221; augmenting yesterday&#8217;s <a title='Javascript instanceof Object Oriented Programming Tutorial' href='#joopt'>Javascript instanceof Object Oriented Programming Tutorial<\/a> attempt to make &#8220;instanceof&#8221; a part of your life.  <font size=1>Like, why aren&#8217;t you taking &#8220;instanceof&#8221; to the zoo today?  After all, the beach outing yesterday was a huge success, I hear?!<\/font>  They are &#8230;<\/p>\n<ul>\n<li>use of <i>inheritance<\/i> ideas in Object Oriented Programming (or <a target=_blank title='OOP information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming'>OOP<\/a>) <font size=1>&#8230; is always a useful idea to get your head around<\/font><\/li>\n<li>the use of &#8220;instanceof&#8221; operator to make a <b>game decision<\/b><\/li>\n<li>nesting an &#8220;instanceof&#8221; operator test <i><b>within Javascript <a target=_blank href='http:\/\/www.w3schools.com\/jsref\/jsref_eval.asp' title='Javascript eval'>eval<\/a><\/b><\/i><\/li>\n<li><b><font color=blue>(global) arrays of Objects<\/font><\/b> can be that moving feast for &#8220;instanceof&#8221; testing<\/li>\n<li>use of Emoji food content via &#8220;class&#8221; (this time in relation to) CSS syntax like &#8230;<br \/>\n<code><br \/>\n&lt;style&gt;<br \/>\n.f1:before { content: '\\01f371 bento box' }<br \/>\n.f2:before { content: '\\01f358 rice cracker' }<br \/>\n.f3:before { content: '\\01f359 rice ball' }<br \/>\n.f4:before { content: '\\01f35a cooked rice' }<br \/>\n.f5:before { content: '\\01f35b curry rice' }<br \/>\n.f6:before { content: '\\01f35c steaming bowl' }<br \/>\n.f7:before { content: '\\01f35d spaghetti' }<br \/>\n.f8:before { content: '\\01f360 roasted sweet potato' }<br \/>\n.f9:before { content: '\\01f362 oden' }<br \/>\n.f10:before { content: '\\01f363 sushi' }<br \/>\n.f11:before { content: '\\01f364 fried shrimp' }<br \/>\n.f12:before { content: '\\01f365 fish cake with swirl' }<br \/>\n.f13:before { content: '\\01f361 dango' }<br \/>\n.f14:before { content: '\\01F366 soft ice cream' }<br \/>\n.f15:before { content: '\\01F367 shaved ice' }<br \/>\n.f16:before { content: '\\01F368 ice cream' }<br \/>\n.f17:before { content: '\\01F369 doughnut' }<br \/>\n.f18:before { content: '\\01F36A cookie' }<br \/>\n.f19:before { content: '\\01F382 birthday cake' }<br \/>\n.f0:before { content: '\\01F370 shortcake' }<br \/>\n&lt;\/style&gt;<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p>And we find it is no coincidence, to us, that this discussion has a &#8220;game&#8221; web application concept behind it.  Games, to us, have concepts, directly associable with (the) &#8220;Nouns&#8221; (of life), and so suit OOP concepts.  This is interesting to us, because the purpose fits the style, whereas with other web applications, we more often, in all honesty, when angsting over how to do it, think of the &#8220;Verbs&#8221; or action points, immediately.  That could just be me, but it could be that this thought process could be an indicator of how you should style, at least the Javascript, of your client side Javascript code, associated with a web application you are working on.  Rest assured, though, that whatever you decide, this decision is just a &#8220;programming lifestyle&#8221; decision.  Don&#8217;t let anyone insist that one or other of OOP (Nouns) versus Function based (Verbs) Javascript methods has to be used for any given solution you decide upon for a web application job.  Just like with mobile phones, you have to know that programming started without OOP, as telephony did without mobile phones.  Just be happy programming, is my advice, for what its worth <font size=1>129 bitcoin last time I looked<\/font>.<\/p>\n<p>So Classes can inherit.  This means you can, as we do with today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/food_oops.html_GETME\" title=\"food_oops.html\">food_oops.html<\/a>&#8216;s Javascript code, we have an action (and data member) packed &#8220;parent&#8221; Class (called Food today) that you might want to consider <i>Subclasses<\/i> (with names like Shortcake or BentoBox) inherit from in the Javascript (very OOPy) code snippet below &#8230;<\/p>\n<p><code><br \/>\nfunction Food(name) {<br \/>\n  this.name = name;<br \/>\n  this.Show = function(cla, pnum) {<br \/>\n    if (cla == '') cla='f1';<br \/>\n    var ss=document.getElementsByTagName('span');<br \/>\n    if (eval(ss.length % 2) == 0) {<br \/>\n    foodsel=this.name;<br \/>\n    document.getElementById('mydiv' + pnum).innerHTML='&lt;span style=\"display:none;border:1px solid red;\" id=s' + ss.length + ' class=' + cla + '&gt;&lt;\/span&gt;&lt;br&gt;' + document.getElementById('mydiv' + pnum).innerHTML;<br \/>\n    document.getElementById('tinstructions').innerHTML=document.getElementById('dname' + eval(3 - eval('' + pnum))).innerHTML + ', have a go at guessing what food ' + document.getElementById('dname' + pnum).innerHTML + ' chose.';<br \/>\n    document.getElementById('opt' + eval(3 - eval('' + pnum))).innerHTML='Please select food ' + document.getElementById('dname' + pnum).innerHTML + ' chose below ...';<br \/>\n    } else {<br \/>\n    var tscore=eval(document.getElementById('score' + pnum).innerHTML.split('Score: ')[1].split('\/')[0]);<br \/>\n    var tgoes=eval(document.getElementById('score' + pnum).innerHTML.split('Score: ')[1].split('\/')[1]);<br \/>\n    tgoes++;<br \/>\n    var found=false;<br \/>\n    try {<br \/>\n      <i><b>eval(<\/i>\"found=(<font color=blue>morsel[\" + eval(pnum % 2) + \"]<\/font> instanceof \" + retclass(this.name) + \");\"<i>)<\/i>;<\/b><br \/>\n    } catch (eee) {<br \/>\n      found=false;<br \/>\n    }<br \/>\n    if (found) { \/\/ if (this.name == foodsel) {<br \/>\n      tscore+=classes.length;<br \/>\n      document.getElementById('tinstructions').innerHTML='Well done, ' + document.getElementById('dname' + pnum).innerHTML + '. Who wants to select next?';<br \/>\n    } else {<br \/>\n      tscore=tscore;<br \/>\n      document.getElementById('tinstructions').innerHTML='Bad luck, ' + document.getElementById('dname' + pnum).innerHTML + '. Who wants to select next?';<br \/>\n    }<br \/>\n    document.getElementById('score' + pnum).innerHTML='Score: ' + tscore + '\/' + tgoes;<br \/>\n    foodsel='';<br \/>\n    document.getElementById('s' + eval(-1 + ss.length)).style.display='inline';<br \/>\n    document.getElementById('mydiv' + pnum).innerHTML='&lt;span id=s' + ss.length + ' class=' + cla + '&gt;&lt;\/span&gt;&lt;br&gt;' + document.getElementById('mydiv' + pnum).innerHTML;<br \/>\n    document.getElementById('opt1').innerHTML='Please select your food below ...';<br \/>\n    document.getElementById('opt2').innerHTML='Please select your food below ...';<br \/>\n    }<br \/>\n  };<br \/>\n}<br \/>\n<br \/>\nfunction Shortcake(name) {<br \/>\n  Food.<a target=_blank title='Javascript call information from W3schools' href='https:\/\/www.w3schools.com\/js\/js_function_call.asp'>call<\/a>(this, name);<br \/>\n}<br \/>\n<br \/>\nfunction BentoBox(name) {<br \/>\n  Food.call(this, name);<br \/>\n}<br \/>\n<\/code><\/p>\n<p>How does <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/food_oops.html\" title=\"Click picture\">today&#8217;s game<\/a> work?  Two players line up on an equal footing.  One chooses a food of interest, as the other player closes their eyes.  Then that other player can open their eyes and try to guess the food type (not shown <font size=1>&#8230; doh!<\/font>), that if they guess correctly, will score them 20 points (for 20 different food types), else no score for that guess.  Any player can assume either of these two roles in the game, first in, best dressed.<\/p>\n<hr>\n<p id='joopt'>Previous relevant <a target=_blank title='Javascript instanceof Object Oriented Programming Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-instanceof-object-oriented-programming-tutorial\/'>Javascript instanceof Object Oriented Programming Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript instanceof Object Oriented Programming Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.jpg\" title=\"Javascript instanceof Object Oriented Programming Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript instanceof Object Oriented Programming Tutorial<\/p><\/div>\n<p>To understand the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator is to &#8220;get&#8221; a whole lot of what Object Oriented Programming (or <a target=_blank title='OOP information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming'>OOP<\/a>) is about.<\/p>\n<p>You&#8217;ve got your concept of a <i>Class<\/i> which is like a &#8220;blueprint&#8221; for describing the characteristics (data members) and methods of an <i>Object<\/i> (and you can think of an <i>Object<\/i> as quite often like the &#8220;Nouns&#8221; of life).<\/p>\n<p>Using that &#8220;blueprint&#8221; <i>Class<\/i> during a web application, using client side Javascript you might use the <b>&#8220;new&#8221;<\/b> keyword like below, and illustrated in that <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> link above &#8230;<\/p>\n<blockquote cite='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'><p>\nfunction Car(make, model, year) {<br \/>\n  this.make = make;<br \/>\n  this.model = model;<br \/>\n  this.year = year;<br \/>\n}<br \/>\nvar auto = <b>new<\/b> Car(&#8216;Honda&#8217;, &#8216;Accord&#8217;, 1998);<br \/>\n<br \/>\nconsole.log(auto <i>instanceof<\/i> Car);<br \/>\n\/\/ expected output: true<br \/>\n<br \/>\nconsole.log(auto <i>instanceof<\/i> Object);<br \/>\n\/\/ expected output: true\n<\/p><\/blockquote>\n<p> &#8230; you are &#8220;instantiating&#8221; the <i>Class<\/i> &#8220;Car&#8221; to the var<font size=1>iable<\/font> &#8220;auto&#8221; above.  That &#8230;<\/p>\n<p><code><br \/>\nfunction Car(make, model, year) {<br \/>\n  this.make = make;<br \/>\n  this.model = model;<br \/>\n  this.year = year;<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; above is called a &#8220;constructor&#8221; and it can contain more than the &#8220;data members&#8221; it shows above.  It can also define &#8220;methods&#8221;.<\/p>\n<p>Here&#8217;s the good thing about Object Oriented Programming (or OOP) like this.  It begs questions.<\/p>\n<p>What velocity is the <i>Car<\/i> moving?  In the &#8220;blueprint&#8221; <i>Car<\/i> class you would define a &#8220;method&#8221; you might call &#8220;CalculateVelocity&#8221; and then to call that method you might go &#8230;<\/p>\n<p><code><br \/>\nvar car_velocity=auto.CalculateVelocity(distance, time);  \/\/ where v = d \/ t<br \/>\n<\/code><\/p>\n<p>So the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator can be thought of as &#8230;<\/p>\n<blockquote cite='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'><p>\nThe instanceof operator tests whether the prototype property of a constructor appears anywhere in the prototype chain of an object.\n<\/p><\/blockquote>\n<p>And we have found, if you are at the situation, dynamically executing client side Javascript, and are unsure, Javascript try\/catch code like &#8230;<\/p>\n<p><code><br \/>\ntry {<br \/>\n  alert(auto <i>instanceof<\/i> Car);<br \/>\n} catch (eee) {<br \/>\n  alert('false');<br \/>\n}<br \/>\n<\/code><\/p>\n<p> &#8230; might help you out.<\/p>\n<p>Today&#8217;s web application usage of the Javascript <a target=_blank title='Javascript instanceof information, thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/instanceof'>instanceof<\/a> operator uses good old <i><a target=_blank title='Javascript DOM document.write method information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_doc_write.asp'>document.write<\/a>(<a target=_blank title='Javascript prompt window information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_win_prompt.asp'>prompt<\/a>([wording],[default]));<\/i> for that &#8220;in mid air&#8221; prompting feeling to a web application.  It is sort of like a game for two players that goes like this &#8230;<\/p>\n<ol>\n<li>Players navigate to <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html_GETME\" title=\"js_instanceof.html\">js_instanceof.html<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/js_instanceof.html\" title=\"Click picture\">live run<\/a> link<\/li>\n<li>A Javascript prompt window appears and both players can read it but before it is answered, the guessing player should turn away while the other player defines an OOP Class and instantiation example, and then clicks the OK button<\/li>\n<li>At this point the guesser can come back, and try to get inside the other player&#8217;s head, answering and fixing an OOP scenario where any question marks (?) planted there by the web application need to be filled in properly by the guesser in order to receive &#8220;smirking rights&#8221; (no scores today), by clicking the OK Javascript prompt window next presented.<\/li>\n<li>Whether correct or not, the &#8230;<br \/>\n<code><br \/>\n(instantiatedvariablename <i>instanceof<\/i> Classname) = true<br \/>\n<\/code><br \/>\n&#8230; is presented as a conclusion to this round of the game.<\/li>\n<li>The web application restarts, and the players may want to swap roles.<\/li>\n<\/ol>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d36985' onclick='var dv=document.getElementById(\"d36985\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/object\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d36985' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d36999' onclick='var dv=document.getElementById(\"d36999\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/game\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d36999' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are a few salient points behind today&#8217;s &#8220;Food Emoji Guessing Game&#8221; augmenting yesterday&#8217;s Javascript instanceof Object Oriented Programming Tutorial attempt to make &#8220;instanceof&#8221; a part of your life. Like, why aren&#8217;t you taking &#8220;instanceof&#8221; to the zoo today? After &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-instanceof-food-emoji-guessing-game-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,15,28,37],"tags":[281,385,476,477,576,2012,652,2010,875,1761,1000,1319],"class_list":["post-36999","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-games","category-oop","category-tutorials","tag-css","tag-emoji","tag-game","tag-games-2","tag-html","tag-instantiation","tag-javascript","tag-object-oriented-programming","tag-oop","tag-prompt","tag-prompt-popup-box","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36999"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=36999"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36999\/revisions"}],"predecessor-version":[{"id":37014,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/36999\/revisions\/37014"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=36999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=36999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=36999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}