const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers() {
return this._courses.appetizers
},
set appetizers(appetizer) {
return this._courses.push(appetizer);
},
get mains() {
return this._cousers.mains
},
set mains(main) {
return this._cousers.push(main)
},
get desserts() {
return this._cousers.desserts
},
set desserts(dessert) {
return this._cousers.push(dessert)
},
get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
}
},
///
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice,
};
this._courses[courseName].push(dish);
},
///
getRandomDishFromCourse: function(courseName) {
const dishes = this._courses[courseName];
console.log(dishes, courseName);
const randomIndex = Math.floor(Math.random() * dishes.length);
console.log(randomIndex);
return dishes[randomIndex];
},
///
generateRandomMeal: function() {
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}, ${dessert.name}. The price is $${totalPrice}.`
}
}
menu.addDishToCourse('appetizers', 'Caeser salad', 5.45);
menu.addDishToCourse('desserts', 'Cake', 12.50);
let meal = menu.generateRandomMeal();
console.log(meal);
You already use .log()
to your advantage, to inspect to the state of your code, as you can see:
[] 'mains'
0
you have no main courses. Which means, within your getRandomDishFromCourse
method will return undefined
when try to get a random main course
Which results in an error here (within generateRandomMeal method):
const totalPrice = appetizer.price + main.price + dessert.price;
main
is undefined, so doesn’t have a price
property.
Thank you very much, as without the main course)))
It appears that several of your getters and setters refer to properties that don’t exist.
yes, already fixed, thanks))
1 Like