Hello everyone, I have almost completed the Meal Maker project but keep getting an error. The error is:
const randomIndex = Math.floor(Math.random() * dishes.length);
^
TypeError: Cannot read property ‘length’ of undefined
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 desserts (desserts){
this._courses.desserts = desserts;
},
addDishToCourse(courseName,dishName,dishPrice){
const dish = {
name: courseName,
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('main');
const dessert = this.getRandomDishFromCourse('dessert');
const totalPrice = appetizer.price + main.price + this.dessert.price;
return `Dishes: ${appetizers.name}, ${main.name}, ${dessert.name}. Total price is ${totalPrice}. `
}
}
menu.addDishToCourse('appetizers','Onion Rings', 5);
menu.addDishToCourse('appetizers','French Fries', 5);
menu.addDishToCourse('appetizers','Pork Dumplings', 7);
menu.addDishToCourse('mains','Fried Chicken', 12);
menu.addDishToCourse('mains','Pork Bulgogi Bowl', 13);
menu.addDishToCourse('mains','Pizza', 8);
menu.addDishToCourse('desserts','Chocolate Cake', 6);
menu.addDishToCourse('desserts','Cheesecake', 6);
menu.addDishToCourse('desserts','Ice cream', 4);
let meal = menu.generateRandomMeal();
console.log(meal)
I have looked over this so many times and cant find what is wrong with it.