Okay, I just finished Meal Maker after some real confusion and hours of beating my head against a wall. I still think I’m either missing something, or that the directions have superfluous steps. I was able to get the code to run, but I have three questions.
1)When do we want to use getters and setters exactly? It’s not clear to me yet.
2)We create getters and setters in this example, but I don’t see where they’re used. It seems to me that addDishToCourse(), getRandomDishFromCourse(), and generateRandomMeal() do all the work without ever making a call to the getters and setters we wrote? Am I missing something in the way this code executes, or are we creating unnecessary getters and setters just to confuse me? lol
3)Did I use the proper syntax on my setters? Would it be better to use a unique parameter name, or is it okay to use the same name as what I’m trying to assign to since the parameter only has method level scope?
Here is my code:
const menu = {
_courses: {
appetizers:[],
mains:[],
desserts:[]
},
get appetizers() {
return this._courses.appetizers;
},
get mains() {
return this._courses.mains;
},
get desserts() {
return this._courses.desserts;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
set mains(mains) {
this._courses.mains = mains;
},
set desserts(desserts) {
this._courses.desserts = desserts;
},
get courses(){
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts
};
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
}
return this._courses[courseName].push(dish)
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse('appetizers');
const main = this.getRandomDishFromCourse('mains');
const dessert = this.getRandomDishFromCourse('desserts');
const totalPrice = appetizer.price + main.price + dessert.price;
return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. The total price is ${totalPrice}.`;
}
};
menu.addDishToCourse("appetizers", "salad", 4.00);
menu.addDishToCourse("appetizers", "wings", 9.00);
menu.addDishToCourse("appetizers", "nachos", 7.00);
menu.addDishToCourse("appetizers", "cheese sticks", 5.00);
menu.addDishToCourse("appetizers", "fries", 3.00);
menu.addDishToCourse("mains", "steak", 17.50);
menu.addDishToCourse("mains", "salmon", 14.50);
menu.addDishToCourse("mains", "pizza", 12.00);
menu.addDishToCourse("mains", "meatloaf", 10.50);
menu.addDishToCourse("mains", "tacos", 9.00);
menu.addDishToCourse("mains", "enchiladas", 10.50);
menu.addDishToCourse("desserts", "tiramisu", 11.00);
menu.addDishToCourse("desserts", "apple crisp", 8.00);
menu.addDishToCourse("desserts", "molten chocolate cake", 12.50);
const meal = menu.generateRandomMeal();
console.log(meal);