Hello everyone,
I’m encountering an issue with the meal meaker, here’s my code :
const menu = {
_courses: {
appetizers: [],
mains: [],
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;
},
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}, ${dessert.name}. The price is ${totalPrice}.`;
},
};
menu.addDishToCourse('appetizers', 'Mozarella', 3.70);
menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Soup', 7.00);
menu.addDishToCourse('Mains', 'Fried Rice', 10.20);
menu.addDishToCourse('Mains', 'Mie Goreng', 12.15);
menu.addDishToCourse('Mains', 'Pepes Ikan', 21.20);
menu.addDishToCourse('desserts', 'Chocolate cake', 7.50);
menu.addDishToCourse('desserts', 'Crème brulée', 7.60);
menu.addDishToCourse('desserts', 'Fruits', 14.20);
const meal = menu.generateRandomMeal();
console.log(meal)
So until I add the dishes to the course at the very end, everything’s fine. But after that, I get an error, that the .push() line 39 is not defined …
I really don’t get how it ‘becomes’ undefined at the very last step ?
If anybody can help, I’ll be very grateful
Thanks a lot !