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

Hi there,
I am having the same problem. Don’t know what is wrong with the addDishToCourse() method !

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(appetizers) {
  this._courses.appetizers = appetizers
},
set mains(mains) {
  this._courses.appetizers = mains
},
set desserts(desserts) {
  this._courses.appetizers = 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 mean is ${appetizer.name}, ${main.name} and ${dessert.name}, and the total price is ${totalPrice}`
}
};

menu.addDishToCourse('appetizers','salad',3.00);
menu.addDishToCourse('appetizers','salad',6.00);
menu.addDishToCourse('appetizers','salad',9.00);
menu.addDishToCourse('mains','steak',12.00);
menu.addDishToCourse('mains','salmon',15.00);
menu.addDishToCourse('mains','tofu',18.00);
menu.addDishToCourse('ice-cream','salad',21.00);
menu.addDishToCourse('banana','salad',24.00);
menu.addDishToCourse('cake','salad',27.00);

const meal = menu.generateMeal();
console.log(meal);

Not sure if you figured this one out yet. But the issue comes here:

menu.addDishToCourse(‘ice-cream’,‘salad’,21.00);
menu.addDishToCourse(‘banana’,‘salad’,24.00);
menu.addDishToCourse(‘cake’,‘salad’,27.00);

The ice-cream, banana, cake should all read as desserts, since that is the courseName.

at the end you declare the variable:
const meal = menu.generateMeal();
But you forgot to place the Random word between:
const meal = menu.generateRandomMeal();