Help With Meal Maker Project

I need help on the Meal Maker project. I coded it and then watched the video to compare and I’m still getting an error message.

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(appetizerIn) {
   this._courses.appetizers = appetizers;
  },
  set mains(mainsIn) {
    this._courses.mains = mains;
  },
  set desserts(dessertsIn) {
   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,
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.Random() * dishes.length); 
    return dishes[randomIndex];
  },
  generateRandomMeal() {
    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}, and ${dessert.name}. The price is ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Salad', 4);
menu.addDishToCourse('appetizers', 'Shrimp', 6);
menu.addDishToCourse('appetizers', 'Soup', 5);

menu.addDishToCourse('mains', 'Spaghetti', 12);
menu.addDishToCourse('mains', 'Tofu', 8);
menu.addDishToCourse('mains', 'Sushi', 15);


menu.addDishToCourse('dessert', 'Pie', 4);
menu.addDishToCourse('dessert', 'Cake', 6);
menu.addDishToCourse('dessert', 'Fruit Salad', 6);

const meal = menu.generateRandomMeal();

console.log(meal);

The error message is regarding the line 37 code:
return this._courses[courseName].push(dish);

Here’s the error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
    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:37:37)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:62: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)```

I believe you’ve got a typo. In your generateRandomMeal() method you have:

const dessert = this.getRandomDishFromCourse('desserts');

Notice that desserts is plural. However, when you add your dishes you have:

menu.addDishToCourse('dessert', 'Pie', 4);
menu.addDishToCourse('dessert', 'Cake', 6);
menu.addDishToCourse('dessert', 'Fruit Salad', 6);

all typed in singular dessert

Thank you so much! I was convinced the error was near line 37 that I didn’t even look at the end of the code.

Sometimes you have to trace back your steps :smiley:
The error in line 37 is that you cannot push undefined. Why is it undefined? That’s when I checked where you were trying to define dish, then how you added dishes to the course, and finally what you were looking for and what you actually input!

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

This is a common error. The context is wrong. appetizers, mains, and desserts are properties of _courses, not menu.

this._courses.appetizers

&c.

Thanks for the details on your method. I’m new to the programming side of this and it’s quite complex. I’m a graphic designer and I know HTML and CSS but JavaScript is my blind spot.