Undefined .push in meal maker project


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?r=master

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:40
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:40:30)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:60: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)

```

let menu = {
_courses: {
_appetizers: [],
_mains: [],
_desserts: [],
},

get appetizers() {

},
set appetizers(appetizersIn) {

},
get mains() {

},
set mains(mainsIn) {

},
get desserts() {

},
set desserts(dessertsIn) {

},

get courses() {
return {
appetizers: this._courses.appetizers,
mains: this._courses.mains,
desserts: this._courses.desserts
};
},

addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
name: ‘dishName’,
price: ‘dishPrice’
};
this._courses[courseName].push(dish);
},

getRandomDishFromCourse(courseName) {
let dishes = this._courses[courseName];
let randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},

generateRandomMeal: function() {
let appetizers = this.getRandomDishFromCourse(‘appetizers’);
let mains = this.getRandomDishFromCourse(‘mains’);
let desserts = this.getRandomDishFromCourse(‘desserts’);
let totalPrice = appetizers.price + mains.price + desserts.price;

return `Your meal is ${appetizers.name}, ${mains.name}, and ${desserts.name}. The price is $${totalPrice}.`;

}

};

menu.addDishToCourse(‘appetizers’, ‘Caesar Salad’, 4.25);
let meal = menu.generateRandomMeal();
console.log(meal);

<do not remove the three backticks above>

why am i getting this error?

#2

That should be a property name (method) associated with a function.

prop: function (...) {

}

Same applies to the getRandomDishFromCourse method.


#3

thank you, fiddled around with it and its okay now.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.