Code-Along: Food Picker
In this code-along, create a website to help visitors decide which food they should eat!
Setting Up
Click here to go to the Food Picker Start project.
- Copy the project
- Run the project
- Click the "Get Food" button
- Realize that nothing is happening
- Panic
- Realize that these instructions will show how to make the button work
- Feel a wave of relief wash over you
Hooking Up the Button
The first step is to add basic functionality to the button.
- Open the index.html file
- Notice the
<button>has anonclickthat calls thegetFoodfunction- But it has yet to be defined!
- Open the script.js file for editing
- Scroll down to the "Add New Code Below" comment
- Make a new line under the comment
- Define a function named
getFood - In the body of the function, add an
alertthat says "Calculating"
Run the project again, click the button, and verify that the pop-up message appears! The code should look something like this:
function getFood() {
alert("Calculating...");
}
Getting a Random Item
Next, find a random item. There are a few things to notice in the existing code:
- The
itemsarray contains various food objects, that all contain some properties - The
getRandomIntfunction will return a random integer between0and themax
The code will have to find a random item using the existing code.
- Make a new line in the body of the
getFoodfunction - Create a new variable named
randomItemIndex - Set
randomItemIndexto be a call togetRandomInt - Pass in
items.lengthto thegetRandomIntcall - Under that, create a new variable named
item - Set
itemto be the value from theitemsarray at therandomItemIndexindex
The code should look something like this:
let randomItemIndex = getRandomInt(items.length);
let item = items[randomItemIndex];
This will not change the functionality of the button yet, but now we have a random item!
Displaying the Random Item Name
Now it is time to display the item information.
- Open the index.html file
- Notice there is a
<p>element with anidoffood-name- This can store the name of the random food item!
- Open the script.js file for editing
- Make a new line in the body of the
getFoodfunction - Create a new variable named
foodNameElement - Set
foodNameElementto select the<p id="food-name">element- Using
document.querySelector
- Using
- Under that, create a new variable named
foodNameText - Set
foodNameTextto beITEM (STORE) - Make the string into a template literal
- In the literal, replace
ITEMwith${item.name}- This gets the
nameproperty of theitemobject
- This gets the
- In the literal, replace
STOREwith{$item.store}- This gets the
storeproperty of theitemobject
- This gets the
- Set the
textContentproperty offoodNameElementtofoodNameText- This sets the actual element text so it will appear on the page
Run the project, click the button, and verify that a random food item name is properly displayed! The additional code should look something like this:
let foodNameElement = document.querySelector("#food-name");
let foodNameText = `${item.name} (${item.store})`;
foodNameElement.textContent = foodNameText;
Displaying the Random Item Picture
The name is good, but it would be much more appetizing with a picture. Luckily, there are some pictures accessible via URL.
- Open the index.html file
- Notice there is a
<p>element with anidoffood-pic- This can store the name of the random item picture!
- Open the script.js file for editing
- Make a new line in the body of the
getFoodfunction - Create a new variable named
foodPictureElement - Set
foodPictureElementto select the<p id="food-pic">element- Using
document.querySelector
- Using
- Under that, create a new variable named
foodPictureUrl - Set
foodPictureUrlto behttps://raw.githubusercontent.com/hytechclub/web-103/main/Assets/URL - Make the string into a template literal
- In the literal, replace
URLwith${item.picture}- This gets the
pictureproperty of theitemobject
- This gets the
- Set the
srcproperty offoodPictureElementtofoodPictureUrl- This sets the actual image URL so that it displays on the page
Run the project, click the button, and verify that a random food item picture is properly displayed! The additional code should look something like this:
let foodPictureElement = document.querySelector("#food-pic");
let foodPictureUrl = `https://raw.githubusercontent.com/hytechclub/web-103/main/Assets/${item.picture}`;
foodPictureElement.src = foodPictureUrl;
Creating a Greek Salad Object
The page should now be fully functional! All that's left is adding some additional food items. Start by creating a new object for a greek salad from Panera.
- Make a new line at the bottom of the script.js file
- Make sure it's outside of the
getFoodfunction (after the})
- Make sure it's outside of the
- There, create a new variable named
greekSalad - Set
greekSaladto be a new empty object with{and}- Press Enter to automatically add space between
- In the object, create a
storeproperty set to"Panera"- Property name, colon, property value, comma
- Under that in the object, create two more properties:
priceset to10pictureset togreeksalad.jpg
The new code should look something like this:
let greekSalad = {
store: "Panera",
price: 10,
picture: "greeksalad.jpg"
};
It will not change the functionality of the site yet - the item must be added to the items array!
Adding the Greek Salad Object
Now the greekSalad is almost ready, but there are a couple things still needed before it will show up as a food item.
- Make a new line at the bottom of the script.js file
- There, set the
nameproperty of thegreekSaladobject to be"Greek Salad"- This ensures that it has a name for the display!
- Under that, push the
greekSaladobject to theitemsarray
At this point:
- The
nameproperty has been added to thegreekSaladobject - The
greekSaladobject has been added to theitemsarray
Run the project, click the button a few times, and verify that the Greek Salad eventually appears! Note that it may be helpful to remove the alert statement from the top of the getFood function for usability purposes. The site also runs slowly sometimes, so if the image is not changing, it may be necessary to wait several seconds.
The additional code should look something like this:
greekSalad.name = "Greek Salad";
items.push(greekSalad);
Creating and Adding a Mac n Cheese Object
Now it will be possible to add new items in a few different ways! Add a Mac n Cheese item to the menu.
- Make a new line at the bottom of the script.js file
- There, create a new variable named
mac - Set
macto be a new empty object with{and}- Press Enter to automatically add space between
- In the object, create a
storeproperty set to"Panera"- Property name, colon, property value, comma
- Under that in the object, create a
nameproperty set to"Mac n Cheese" - Under that in the object, create a
priceproperty set to7 - Under that in the object, create a
pictureproperty set to"macncheese.jfif"- Note that there is a file with the URL
https://raw.githubusercontent.com/hytechclub/web-103/main/Assets/macncheese.jfif
- Note that there is a file with the URL
- Under that, outside the object (after
}), make a new line - There, push the
macobject to theitemsarray
Run the project, click the button a few times, and verify that the Mac n Cheese eventually appears! The additional code should look something like this:
let mac = {
store: "Panera",
name: "Mac n Cheese",
price: 7,
picture: "macncheese.jfif"
};
items.push(mac);
Creating and Adding a Soup Object
It is also possible to add a new item directly without creating a separate variable! Add Soup to the possible Panera food items in this manner.
- Make a new line at the bottom of the script.js file
- Push a currently empty object to the
itemsarray - Add a
storeproperty to the pushed object set to"Panera" - Add a
nameproperty to the pushed object set to"Soup" - Add a
priceproperty to the pushed object set to6 - Add a
pictureproperty to the pushed object set to"panerabread.jpg"
Run the project, click the button a few times, and verify that the Soup eventually appears! The additional code should look something like this:
items.push({
name: "Soup",
price: 6,
picture: "panerabread.jpg"
});
Conclusion
That's it for the code-along activity! At this point, it should be possible to click the button, and see a variety of different foods appear.
The added code in the script.js file should look something like this:
function getFood() {
let randomItemIndex = getRandomInt(items.length);
let item = items[randomItemIndex];
let foodNameElement = document.querySelector("#food-name");
let foodNameText = `${item.name} (${item.store})`;
foodNameElement.textContent = foodNameText;
let foodPictureElement = document.querySelector("#food-pic");
let foodPictureUrl = `https://raw.githubusercontent.com/hytechclub/web-103/main/Assets/${item.picture}`;
foodPictureElement.src = foodPictureUrl;
}
let greekSalad = {
store: "Panera",
price: 10,
picture: "greeksalad.jpg"
};
greekSalad.name = "Greek Salad";
items.push(greekSalad);
let mac = {
store: "Panera",
name: "Mac n Cheese",
price: 7,
picture: "macncheese.jfif"
};
items.push(mac);
items.push({
store: "Panera",
name: "Soup",
price: 6,
picture: "panerabread.jpg"
});