Meal Maker error with .push()

I’ve finished the code for the Meal Maker exercise, but I keep getting an error with using the .push() method suggested in step 8. I’ve reviewed my code against the hints and also the solutions posted here, and I’m still stuck. The specific error is ‘TypeError: Cannot read property ‘push’ of undefined’.

Code base below:

const menu = {
  _courses: {
    appetizers : [],
    mains : [],
    desserts : []
  },
  
  get appetizers() {
   return this._courses.appetizers;
  },
  set appetizers(appetizerIn) {
   return this._courses.appetizers = appetizerIn;
  },
  get mains() {
    return this._courses.mains;
  },
  set mains(mainIn) {
   return this._courses.mains = mainIn;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(dessertIn) {
   return this._courses.desserts = dessertIn;
  },
  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 randNum = Math.floor(Math.random()*dishes.length);
    return dishes[randNum];
  },
  generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totPrice = appetizer.price + main.price + dessert.price;
    return `Your meal today will be ${appetizer.name}, followed by ${main.name}, and with ${dessert.name} for dessert. The total price of today's meal will be ${totPrice}.`;
  }
}

menu.addDishToCourse('appetizers', 'Fries', 2.50);
menu.addDishToCourse('appetizers', 'Onion Rings', 3.50);
menu.addDishToCourse('appetizers', 'Avocado Toast', 10.00);
menu.addDishToCourse('mains', 'Steak', 22.50);
menu.addDishToCourse('mains', 'Gnocchi', 15.00);
menu.addDishToCourse('mains', 'Milksteak', 100.00);
menu.addDishToCourse('dessert', 'Ice Cream', 5.00);
menu.addDishToCourse('dessert', 'Cake', 6.99);
menu.addDishToCourse('dessert', 'Lemonchello', 3.50);

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

Any help is much appreciated.

1 Like

Hello, @digital9451444528.

Welcome to the forums.

At what point do you receive the error? Consider what the error message is telling you. undefined means the computer doesn’t know what the thing is that you’ve asked it to find. You use .push() here:

this._courses.courseName.push(dish);

If you follow this code carefully for each courseName you pass to the addDishToCourse() method, I think you’ll find the undefined courseName.

Also, when you post code, the button you want is:
</>

1 Like