Hi, I hope someone can explain what the problem is, as I am struggling to understand this.
Here is a link to my code in the JS Objects Meal Maker Project.
I have both used the hints and watched the video walk through for this project, and can’t see what I am doing differently to them (the video and hints had some differences to each other, but I tried following each way) - yet I am getting an error.
TypeError Message:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:42
this._courses[courseName].push(dish);
^
TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:42:29)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:65:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
My code:
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers() {
return this._courses.appetizers;
},
get mains() {
return this._courses.mains;
},
get desserts() {
return this._courses.desserts;
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers;
},
set mains(mains) {
this._courses.mains = mains;
},
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,
};
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
const random = Math.floor(Math.random() * dishes.length);
return dishes[random];
},
generateRandomMeal() {
const appetizer = getRandomDishFromCourse("appetizers");
const main = getRandomDishFromCourse("mains");
const dessert = getRandomDishFromCourse("desserts");
const totalPrice = appetizer.price + main.price + dessert.price;
return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}: $${totalPrice}`;
}
};
menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Garlic Bread', 5.75);
menu.addDishToCourse('appetizers', 'Dip', 2.15);
menu.addDishToCourse('main', 'Lasagne', 11.00);
menu.addDishToCourse('main', 'Steak', 15.25);
menu.addDishToCourse('main', 'Tacos', 12.95);
menu.addDishToCourse('dessert', 'Yoghurt', 4.25);
menu.addDishToCourse('dessert', 'Cheesecake', 7.25);
menu.addDishToCourse('dessert', 'Chocolate', 6.25);
const meal = menu.generateRandomMeal();
console.log(meal);