Meal Maker Project - Cannot read property 'push' of undefined


#1

I’ve went through the code multiple times, also compared it with what is given in the hints yet I’m failing to complete the project. Couldn’t find a working version on the forum (following the exact steps & hints from the project) so I can understand what am I doing wrong…

Can someone help me out? Thank you

P.S. Why isn’t there a project category for this project on the forum?

Project link: https://www.codecademy.com/en/courses/learn-javascript-objects/projects/meal-maker

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-javascript-objects-meal-maker/app.js:37:30)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:53: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(){
      
    },
    set appetizers(appetizerIn){
      
    },
    get mains(){
      
    },
    set mains(mainIn){
      
    },
    get desserts(){
      
    },
    set desserts(dessertIn){
      
    }
  },
  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(){
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let dessert = this.getRandomDishFromCourse('desserts');
    let totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}. The price is $ ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers','Salad', 3.50);
menu.addDishToCourse('appetizers','Bruschetti', 7);
menu.addDishToCourse('appetizers','Spiced Olives', 5.80);

menu.addDishToCourse('mains', 'Grilled Chicken', 12.89);
menu.addDishToCourse('mains', 'Pork Ribs', 13.20);
menu.addDishToCourse('mains', 'Grilled fish', 10.40);

menu.addDishToCourse('desserts', 'Cheesecake', 5);
menu.addDishToCourse('desserts', 'Chocolate cake', 8);
menu.addDishToCourse('desserts', 'Ice Cream', 4);

let meal = menu.generateRandomMeal();
console.log(meal);

UPDATE
Received help from Galina in the chat, so the problem is with the getters and the setters which are not defined. I think the instructions should’ve be more insightfull.

Suggestion for staff: At least please add some dots(…) in the step 4 hint - I thought they had to be empty bcz of how the hint is presented.

hint_step_4_meal_mkr

The code for the getters/setters ended up like below:

get appetizers(){
      return this._appetizers;
    },
    set appetizers(appetizerIn){
      return this._appetizers.push(appetizerIn);
    },
    get mains(){
      return this._mains;
    },
    set mains(mainIn){
      return this._mains.push(mainIn);
    },
    get desserts(){
      return this._desserts;
    },
    set desserts(dessertIn){
      return this._desserts.push(dessertIn);
    }

#2

Create getter and setter methods for the appetizers, mains, and desserts properties.

Above, in the last code sample, there is no get appetizers getter. The setter for this property needs it to complete its job.


#3

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