{"id":3770,"date":"2023-07-18T01:22:44","date_gmt":"2023-07-18T01:22:44","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3770"},"modified":"2026-02-24T00:53:46","modified_gmt":"2026-02-24T00:53:46","slug":"exercise-objects","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-six\/exercise-objects\/","title":{"rendered":"Exercise: Objects"},"content":{"rendered":"\n<p>As you learned in your freecodecamp.org lessons, Javascript objects store information through &#8216;name value&#8217; pairs. In plain English, this simply means that information is retrieved from an object by asking for it by name. You should be pretty familiar with these concepts after going through the lessons so let&#8217;s go ahead and put that knowledge to use in yet another lesson.<\/p>\n\n\n\n<p>To begin, download&nbsp;this file named <a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/09\/objects.zip\" data-type=\"attachment\" data-id=\"4515\">objects.zip<\/a>.&nbsp; Once the file is unzipped, you&nbsp; should see a file named beverage.html and a js folder with another file inside named beverage.js. You will also have a &#8216;beveragepics&#8217; folder with five beverage options. Move everything to wherever you are doing your work for this class and open both files simultaneously with your code editor.<\/p>\n\n\n\n<p>As you can see some of your work is already done: both files have content and are connected via a script tag in beverage.html. In addition:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>beverage.html has a button that calls a (presently empty) function in beverage.js<\/li>\n\n\n\n<li>beverage.js contains an object named &#8216;drinkMenu&#8217; that attaches a description to each drink option.<\/li>\n\n\n\n<li>In addition to drinkMenu, beverage.js also contains an array with all of beverage names.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Retrieve descriptions via text entry<\/h4>\n\n\n\n<p>Goal: Type a drink name into the text field, click the button, and display the drink description. <\/p>\n\n\n\n<p>For our first exercise we are going to use the text field and button that already exists to pull quotes from drinkMenu and make them appear inside of the h2 tags in the html document (id of &#8220;#aboutme&#8221;). This can be accomplished by adding just two lines of code to the serveDrink function. I am going to offer general instructions and your job is to figure out the rest.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a variable (call it&nbsp;<strong>sName<\/strong>&nbsp;) inside of the serveDrink function. Use document.querySelector to set the value of sName to the value of the text field (which, as you can see in the html file, has an id of &#8220;#adrink&#8221;)<\/li>\n\n\n\n<li>Use document.querySelector again to set the innerHtml property of #aboutme (tied to the &lt;h2&gt; tags) to the appropriate value in drinkMenu. <\/li>\n\n\n\n<li>Finally, add a conditional (if, else) statement with that displays the drink <strong>if<\/strong> the input matches a drink in the drinkMenu object. If the user types anything <strong>else<\/strong>, they will receive an error message (example: &#8220;Lemonade isn&#8217;t a valid drink.&#8221;). <\/li>\n<\/ol>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Need a hint? Check it out here:<\/summary>\n<pre class=\"wp-block-code\"><code>function serveDrink() {\n    <strong>\/\/ Step 1: Get the drink name from the input field<\/strong>\n    let sName = document.querySelector('#adrink').value;\n\n    <strong>\/\/ Step 2: Check if the drink is in the drinkMenu object<\/strong>\n    if (drinkMenu.hasOwnProperty(sName)) {\n        \/\/ <strong>Step 3: Show the drink description<\/strong>\n        document.querySelector('#aboutme').innerHTML = drinkMenu&#091;sName];\n    } else {\n        \/\/ <strong>Step 4: Error message for invalid entry<\/strong>\n        document.querySelector('#aboutme').innerHTML = sName + \" isn't a valid drink.\";\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p>Test it out! Type &#8220;Latte,&#8221; click the button, and the description should appear. Type &#8220;Lemonade,&#8221; click the button, and the error message should appear. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Retrieve descriptions from a dropdown list.<\/h4>\n\n\n\n<p>Hopefully, you are impressed with your project so far, but wouldn&#8217;t it be even better if the drink options were part of a dropdown list? Luckily, you know Javascript and you have an array of those drink names just waiting to be put to use, with just a tiny bit of coding. So let&#8217;s do it!<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Either delete or comment out the button and textfield in the html file, and replace them with a select field. <pre class=\"wp-block-code\"><code><strong>&lt;!-- Instead of button and input: --&gt;<\/strong><br>&lt;h1&gt;Select a Drink&lt;\/h1&gt;<br>&lt;select id=\"adrink\" onchange=\"serveDrink()\"&gt;<br>&lt;\/select&gt;<\/code><\/pre><\/li>\n\n\n\n<li>As you can see, you will set the id of the select field to &#8220;#aname&#8221; and use &#8216;onchange&#8217; to call the same quote function that you used in the previous exercise.<\/li>\n\n\n\n<li>Return to beverage.js. Just after the drinkArray but OUTSIDE of the serveDrink function, notice the variable named drinkList with a value of #adrink (via querySelector of course). <pre class=\"wp-block-code\"><code>let drinkList = document.querySelector(\"#adrink\");<span style=\", serif\"><\/span><\/code><\/pre><\/li>\n\n\n\n<li>At the top of beverage.js, add a display map to provide cleaner label options. The display map looks like this: <pre class=\"wp-block-code\"><code>let drinkDisplayMap = {\n    \"Latte\": \"Latte\",\n    \"IcedMatchaLatte\": \"Iced Matcha Latte\",\n    \"ChaiTea\": \"Chai Tea\",\n    \"BubbleTea\": \"Bubble Tea\",\n    \"ThaiIcedTea\": \"Thai Iced Tea\"\n};<\/code><\/pre><\/li>\n\n\n\n<li>Finally, build a for loop to add the drink options to the dropdown menu. This for loop  uses the internal key that you created in the drinkDisplayMap to display the user friendly version of the drink name.   <pre class=\"wp-block-code\"><code>for (let key in drinkDisplayMap) {<br>    drinkList.innerHTML += `&lt;option value=\"${key}\"&gt;${drinkDisplayMap[key]}&lt;\/option&gt;`;<br>}<\/code><\/pre><\/li>\n<\/ol>\n\n\n\n<p>The serveDrink() function stays the same! Now, it&#8217;s just pulling values from the dropdown drink menu. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add pictures<\/h4>\n\n\n\n<p>It&#8217;s not a menu without photos! The goal for this part of the exercise is to automatically display the matching image of the drink once it is selected. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Notice the div tag underneath the h2 tag where quotes appear with an id of image.<\/li>\n\n\n\n<li>In the serveDrink function, add a line to the conditional statement. <strong><br><\/strong>The code will include a document.querySelector and innerHtml, to make the appropriate image appear in the #image div tag that you created in the previous step. \n<ul class=\"wp-block-list\">\n<li><em>Make sure your img src is correct. Since you are using inner html, the file path will go from beverage.html to the beveragepics folder. <\/em>\n<ul class=\"wp-block-list\">\n<li><em>Take a look at the image names in the &#8216;beveragepics&#8217; folder. What a helpful coincidence that they match the keys in drinkMenu. You know, the same keys that are referenced by aDrink when you select a drink from the dropdown list. <\/em><\/li>\n\n\n\n<li><em>Finally, add a line to the else statement as well that displays no image if a drink is not selected.<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Need a hint? Check it out:<\/summary>\n<pre class=\"wp-block-code\"><code>function serveDrink() {\n    let sName = document.querySelector('#adrink').value;\n\n    if (drinkMenu.hasOwnProperty(sName)) {\n        \/\/ Show description\n        document.querySelector('#aboutme').innerHTML = drinkMenu&#091;sName];\n\n        <strong>\/\/ Show image<\/strong>\n        document.querySelector('#image').innerHTML = \n            \"&lt;img src='beveragepics\/\" + sName + \".png' alt='\" + drinkDisplayMap&#091;sName] + \"' style='max-width: 300px;'&gt;\";\n    } else {\n        document.querySelector('#aboutme').innerHTML = sName + \" isn't a valid drink.\";\n        document.querySelector('#image').innerHTML = \"\";\n    }\n}<\/code><\/pre>\n<\/details>\n\n\n\n<p>Need a challenge? Try these:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a &#8220;Clear&#8221; button<\/li>\n\n\n\n<li>Add a &#8220;Random drink&#8221; button<\/li>\n\n\n\n<li>Add a price with another object!<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As you learned in your freecodecamp.org lessons, Javascript objects store information through &#8216;name value&#8217; pairs. In plain English, this simply means that information is retrieved from an object by asking for it by name. You should be pretty familiar with these concepts after going through the lessons so let&#8217;s go ahead and put that knowledge &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-two-javascript\/week-six\/exercise-objects\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Objects<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3694,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3770","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3770"}],"version-history":[{"count":14,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3770\/revisions"}],"predecessor-version":[{"id":4783,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3770\/revisions\/4783"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3694"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}