Hi everybody, I have this problem and I don’t find the mistake, please help me…
Thank you very much…
const menu = {
_courses : {
appetizers: [],
mains: [],
desserts: []
},
get courses() {
return{
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
}
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
get mains() {
return this._courses.mains;
},
set mains(mains) {
this._courses.mains = mains;
},
get desserts() {
return this._courses.desserts;
},
set appetizers(desserts) {
this._courses.desserts = 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}, ${dessert.name} The total price is ${totalPrice}`;
}
};
menu.addDishToCourse('appetizers','Caesar Salad', 4.25);
let meal = menu.generateRandomMeal();
console.log(meal);
This is the message error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:77
const totalPrice = appetizer.price + main.price + dessert.price;
^
TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:77:36)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:87:17)