Meal Maker, (.push)

const menu ={
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
},
  get courses(){
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    }
  },
  get appetizers(){
    return this._courses.appetizers
  },
  set appetizers(A){
    this._courses.appetizers = A
  },
  get mains(){
    return this._courses.mains
  },
  set mains(M){
    this._courses.mains = M
  },
  get desserts(){
    return this._courses.desserts
  },
  set desserts(D){
    this._courses.desserts = D
  },
  
  addDishToCourse(courseName, dishName, dishPrice){
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish);
  },
 getRandomDishFromCourse(courseName){
    const dishes = this._courses
    const dishlength = Math.floor(dishes.length * Math.random())
    return dishes[dishlength]
  },
  getRandomMeal(){
    const appetizer = this.getRandomDishFromCourse('appetizers')
    const main = this.getRandomDishFromCourse('mains')
    const dessert = this.getRandomDishFromCourse('desserts')
    const totalPrice = appetizer.price + main.price + dessert.price
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name} and price is $${totalPrice}.`
  }
}
menu.addDishToCourse('apple', 'pudding', 30)

This is my code for the Meal Maker, but I’m struggling with this error. Not sure on what to do and how to fix it.

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38
    return 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:38:37)
    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)
addDishToCourse(courseName, dishName, dishPrice){
  const dish = {
  name: dishName,
  price: dishPrice, 
  };
  return this._courses[courseName].push(dish);
},

menu.addDishToCourse(‘apple’, ‘pudding’, 30)

What does addDishToCourse() expect as the first parameter and what are you using as the first argument in your method call?

1 Like