Hi guys!
I am on the Meal Maker exercise and I have the following error when running my code:
_this.courses[courseName].push(dish);
TypeError: Cannot read property ‘push’ of undefined
_ at Object.addDishToCourse_
I can’t see where the mistake is though… Here is my code. Can someone please help?
let menu = {
_courses: {
appetizers: [],
mains: [],
dessert: [],
get appetizers() {
return this._appetizers;
},
get mains() {
return this._mains;
},
get desserts() {
return this._desserts;
},
set appetizers(appetizerIn){
this._appetizers.push(appetizerIn);
},
set mains(mainIn){
this._mains.push(mainIn);
},
set desserts(dessertIn){
this._desserts.push(dessertIn);
}
},
get courses() {
return {
appetizers: this._courses.appetizers,
mains: this._courses.mains,
desserts: this._courses.desserts
}
},
addDishToCourse(courseName,dishName,dishPrice) {
const dish = {
name: dishName,
price: dishPrice
};
this._courses[courseName].push(dish);
console.log(courseName + ' ' + dishName + ' ' + dishPrice);
},
getRandomDishFromCourse: function(courseName) {
const dishes =
this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
},
generateRandomMeal: function() {
const appetizers = this.getRandomDishFromCourse('appetizers');
const mains = this.getRandomDishFromCourse('mains');
const desserts = this.getRandomDishFromCourse('desserts');
const totalPrice = appetizers.price + mains.price + desserts.price;
return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}. The price is $${totalPrice}.`;
}
};
menu.addDishToCourse('appetizers', 'Bruschetta', 5);
menu.addDishToCourse('appetizers', 'Chicken liver parfait', 7);
menu.addDishToCourse('appetizers', 'Smoked salmon', 6);
menu.addDishToCourse('mains', 'Sirloin steak and fries', 15);
menu.addDishToCourse('mains', 'Grilled cod and greens', 12);
menu.addDishToCourse('mains', 'Caesar salad', 10);
menu.addDishToCourse('desserts', 'Cheese selection', 7);
menu.addDishToCourse('desserts', 'Sticky toffee pudding', 6);
menu.addDishToCourse('desserts', 'Apple crumble', 5);```
Thanks for your help!