Hi,
Can someone please help with my code below, if I feel like its got loads of errors in it, but I’m currently getting the following error.
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
** this._courses[courseName].push(dish);**
** ^**
TypeError: Cannot read property ‘push’ of undefined
** at Object.addDishToCourse (/home/ccuser/workspace/learn**
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: []
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(appetizerIn) {
this._courses.appetizers = appetizerIn;
},
get mains() {
return this._courses.mains;
},
set mains(mainsIn) {
this._courses.mains = mainsIn;
},
get desserts() {
return this._courses.desserts;
},
set desserts(dessertIn) {
this._courses.appetizers = dessertIn;
},
get courses() {
return {
appetizers: this.appetizerIn,
mains: this.mainsIn,
desserts: this.dessertIn
}
},
addDishToCourse: function (courseName, dishName, dishPrice) {
const dish = {
name: dishName,
price: dishPrice
}
this._courses[courseName].push(dish);
},
getRandomDishFromCourse: function (courseName) {
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random * dishes.length);
return dishes[randomIndex];
},
generateRandomMeal: function () {
const appetizer = this.getRandomDishFromCourse(this._courses.appetizer);
const main = this.getRandomDishFromCourse(this._courses.main);
const dessert = this.getRandomDishFromCourse(this._courses.dessert);
const totalPrice = courseName[0].price + courseName[1].price + courseName[2].price;
return `Your meal is ${courseName[0].name}, ${courseName[1].name} and ${courseName[2].name} The price is $${totalPrice}.`
}
}
menu.addDishToCourse('appetizer', 'soup', 7);
menu.addDishToCourse('appetizer', 'crab', 9);
menu.addDishToCourse('appetizer', 'bruscetta', 4);
menu.addDishToCourse('main', 'lamb shank', 12);
menu.addDishToCourse('main', 'steak and chips', 16);
menu.addDishToCourse('main', 'lamb curry', 13);
menu.addDishToCourse('dessert', 'ice cream', 5);
menu.addDishToCourse('dessert', 'fudge cake', 6);
menu.addDishToCourse('dessert', 'tiramasu', 4);
let meal = menu.generateRandomMeal();
console.log(meal);
I know that this._courses[courseName].push(dish); is undefined and you cant push to an undefined value, but i have no idea how to solve it, as its the same as the code answer.
Thanks for your help,