Meal Maker Errors

Hi All,

I have been stuck on this meal maker project for a few days, have tried rewriting from scratch a few times but always seem to come to the same error, would greatly appreciate if someone could point out where I’m going wrong!

I am receiving error below, I have seen similar questions posted but I’m not really clear on what is wrong here?

Https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:39
    return this._courses[courseName].push(dish);
                                    ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse
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(appetizer) {
    this._courses.appetizers = appetizer;
  },
  set mains(main) {
    this._courses.mains = main;
  },
  set desserts(dessert) {
    this._courses.desserts = dessert;
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };
  },
  
  //-----------------------------
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const j = Math.floor(Math.random * dishes.length);
    return dishes[j];
  },
  
  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 total price is ${totalPrice}`;
  }
};

menu.addDishToCourse('_appetizers','soup',10);
menu.addDishToCourse('_appetizers','bread',10);
menu.addDishToCourse('_appetizers','olives',10);

menu.addDishToCourse('_mains','steak',10);
menu.addDishToCourse('_mains','chicken',10);
menu.addDishToCourse('_mains','pork',10);

menu.addDishToCourse('_desserts','pie',10);
menu.addDishToCourse('_desserts','ice cream',10);
menu.addDishToCourse('_desserts','waffle',10);

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

Why the underscore in the first argument of the addDishToCourse method calls?

Apologies that is a typo that got copy pasted… Im now getting this error below, if price is undefined then it maybe means the .getRandomDishFromCourse method is failing in some way?

 const totalPrice = appetizer.price + main.price + dessert.price;
                                ^

TypeError: Cannot read property 'price' of undefined

updated 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(appetizer) {
    this._courses.appetizers = appetizer;
  },
  set mains(main) {
    this._courses.mains = main;
  },
  set desserts(dessert) {
    this._courses.desserts = dessert;
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts
    };
  },
  
  //-----------------------------
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const j = Math.floor(Math.random * dishes.length);
    return dishes[j];
  },
  
  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 total price is ${totalPrice}`;
  }
};

menu.addDishToCourse('appetizers','soup',10);
menu.addDishToCourse('appetizers','bread',10);
menu.addDishToCourse('appetizers','olives',10);

menu.addDishToCourse('mains','steak',10);
menu.addDishToCourse('mains','chicken',10);
menu.addDishToCourse('mains','pork',10);

menu.addDishToCourse('desserts','pie',10);
menu.addDishToCourse('desserts','ice cream',10);
menu.addDishToCourse('desserts','waffle',10);

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

You’re slightly misreading the error but you are still on the right track. price is not undefined, appetizer is what is undefined. You can verify this by console logging appetizer before the totalPrice initialization. But are main and dessert also undefined? Yes they are so this tells us that our problem is in .getRandomDishFromCourse like you hypothesized. How to solve the error? You could just stare at it until you find the mistake but that can take hours. Instead start console logging everything in your getRandomDishFromCourse method. If you still have trouble finding the bug just reply back.

2 Likes

I finally spotted it, originally I thought it was the line “return dishes[j]” but after trying an alternate way of writing that, it was missing parentheses all along, thank you for your help and patience!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.