'Meal Maker' project cannot read property 'price' of undefined

Hi! This is the ‘Meal Maker’ project. I’m not sure what is causing the error. Anything helps, thanks!!

const menu = {
  _courses: {
      appetizers: [],
      mains: [],
      desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  get mains() {
    return this._courses.main;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set appetizers(appetizerIn) {
    return this._courses.appetizers = appetizerIn;
  },
  set mains(mainIn) {
    return this._courses.mains = mainIn;
  },
  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,
    };
    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 `With the ${appetizer.name}, the ${main.name}, and the ${dessert.name} the total price is ${totalPrice}. `;
}
};


menu.addDishToCourse('appetizers', '1', 10);
menu.addDishToCourse('appetizers', '2', 9);
menu.addDishToCourse('appetizers', '3', 8);

menu.addDishToCourse('mains', '4', 10);
menu.addDishToCourse('mains', '5', 10);
menu.addDishToCourse('mains', '6', 10);

menu.addDishToCourse('desserts', '7', 10);
menu.addDishToCourse('desserts', '8', 10);
menu.addDishToCourse('desserts', '9', 10);


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



been looking this over. can’t seem to find the what is wrong…

keep getting: 'TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal ’

p.s. was just to lazy to put in dish names

1 Like

Your problem is actually quite a simple one. Your entire code is perfect apart from one little snag! In getRandomDishFromCourse() if you do some console.log 's then you’ll find that your randomIndex is outputting NaN. The reason is that you’ve written Math.random, when it should be Math.random() as random() is a method in the class Math. Add these brackets and everything should work as expected.

1 Like

:smile: wow. That makes sense. I suppose I just anxiously overlooked it. Thank you so much for your help!

1 Like

It happens, 50% of coding is just double checking you’ve got all the brackets closed!