Now that we can write, store, and modify objects, let's look at methods. Methods are functions that are stored on objects. We can create our own methods on objects, but JavaScript provides us with built-in methods that are commonly used.
-
Look over the provided
restaurantobject. Identify its keys and verify which data type is stored for each value.This object does something different from our last restaurant object. It has a
pizzaToppingskey that stores an object with pizza topping keys and price values. -
Write a function,
grabCategories, that accepts an object and returns an array of all the keys it contains. -
Call the function and pass in the
papaJohnsobject. What do you expect the result to be?- When we pass in
papaJohns, the array should contain the keypizzaToppings, but NOT contain the names of the pizza toppings themselves.
- When we pass in
-
Bonus: If we want this function to return the names of all
pizzaToppingsin one array without changing the internal code, what argument would we need to pass it? -
Write a function,
verifyValues, that accepts an object and a target number of values. It returns a boolean representing if the number of values in the object is equal to the input value.Note: When we use
Object.values(), what is returned? Are there any pre-existing properties that can help us check the number of values? -
Run this function passing in the
papaJohnsobject and a target number that will make the resultfalse. Then, run the function with a target number that will yield a result oftrue. -
Write a function,
getToppingsInfo, that intakes any object representing a pizza place and returns an array of smaller arrays containing two elements each: the topping key and its price. You can assume all pizza place objects have a string key"pizzaToppings". -
Now, without changing the previous code, add a
printAdmethod to ourpapaJohnsobject that will announce a sale. It will intake a topping and return the following string:"Welcome to Papa John's! We are located at 555 Main Street. This week, we are having a sale on {topping} for ${topping price}. Better Ingredients. Better Pizza. Papa John's."- Run this function and pass in pizza toppings. Make sure the ad changes and displays the correct price for each one. For example, an input of
'bacon'should announce a sale of bacon for$3.
- Run this function and pass in pizza toppings. Make sure the ad changes and displays the correct price for each one. For example, an input of
-
Bonus: Refactor this function so it can handle any changes in address or slogan.
- Without adjusting the original
papaJohnsobject, update the slogan and address to anything you choose. - Run your refactored method and ensure the new address and slogan are printed in the ad!
- Without adjusting the original