Meal Maker TypeError: Cannot read property 'price'

let menu = {
_courses: {
appetizers: ,
mains: ,
desserts:
},
set appetizers(appetizers) {
this._courses.appetizers = appetizers
},
get appetizers() {
return this._courses.apperizers
},
set mains(mains) {
this._courses.mains = mains
},
get mains() {
return this._courses.mains
},
set desserts(desserts) {
this._courses.desserts = desserts
},
get desserts() {
return this._courses.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 randomNumber = Math.floor(Math.random * dishes.length);
return dishes[randomNumber];
},
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 `So your order is: ${appetizer.name} as an appetizer

{main.name} as your main course and {dessert.name} as a dessert
The total price will be {totalPrice}`
}
};

//appetizers
menu.addDishToCourse(‘appetizers’, ‘Bruschetta’, 8.95);
menu.addDishToCourse(‘appetizers’, ‘Stuffed Mushrooms’, 7.50);
menu.addDishToCourse(‘appetizers’, ‘Shrimp Scampi’, 10);
//main menu
menu.addDishToCourse(‘mains’, ‘Bacon Cheeseburger’, 12);
menu.addDishToCourse(‘mains’, ‘Minestrone Soup’, 8);
menu.addDishToCourse(‘mains’, ‘Philly Steak Sandwich’, 9.50);
//desserts
menu.addDishToCourse(‘desserts’, ‘Caramel Brownie’, 5.95);
menu.addDishToCourse(‘desserts’, ‘Ice Cream Cookie Sundae’, 6.75);
menu.addDishToCourse(‘desserts’, ‘Sundae’, 5.95);

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

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:50
const totalPrice = appetizer.price + main.price + dessert.price
^

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:50:33)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:72:23)
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)

1 Like

The error lies in getRandomDish().

You random number is not picked correctly so it doesn’t return a dish.

getRandomDishFromCourse(courseName) {
     const dishes = this._courses[courseName];
     const randomNumber = Math.floor(Math.random * dishes.length);
     return dishes[randomNumber];
},

Should be:

getRandomDishFromCourse(courseName) {
     const dishes = this._courses[courseName];
     const randomNumber = Math.floor(Math.random() * dishes.length); // you forgot the () behind random
     return dishes[randomNumber];
},

Jeez so shame I missed it, thank you so much!

1 Like

No worries, this was a typical case of a domino effect. I had to look really hard to find it!

Hello! I get this same error, but I included the () for Math.random()… I’m totally stumped! Could you take a look and hopefully find what’s wrong? Thanks!

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) {

  },
  set mains(mainsIn) {

  },
  set desserts(dessertsIn) {

  },
  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}, ${dessert.name}, and the total price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 6.50);
menu.addDishToCourse('appetizers', 'fries', 3.00);

menu.addDishToCourse('mains', 'steak', 15.25);
menu.addDishToCourse('mains', 'salmon', 13.75);
menu.addDishToCourse('mains', 'tofu', 12.00);

menu.addDishToCourse('desserts', 'ice cream sundae', 5.25);
menu.addDishToCourse('desserts', 'tres leche', 8.50);
menu.addDishToCourse('desserts', 'brownies', 4.75);

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

What is the return value of the .push() method? Will take some research. Do that, and come back to this topic so we can point out the error.

Well, I really don’t know how to explain what that returns, other than it is used to add the menu item data to the _courses array. I am brand new at this and I barely understand what is happening.

The suggestion stands, then… Research. Go the MDN resource and find out what the return value of push is.


This is not a bid to be snide. It’s a bid to have you discover the resource that can answer your question, to some degree, and expose the error in your code.

search?Array.prototype.push() mdn

These function calls don’t look right, do they?

What is the proper syntax for supplying an argument in a function call?

The line of code referenced by @mtf won’t throw an error, but it is important to understand what return does, and in this case what the value being returned is. Where is the value returned to?

… and what is expected, if anything?

Coding can get frustrating at times but you’ll get it eventually. You did make a mistake in copying the code from the video :wink: .

All I can say is that yes, there are some bugs here and there in the courses. I report them to my best effort too. Some get picked up, some don’t.

If you feel like not extending your pro membership, that is entirely up to you. But for me personally I have learned a lot from the courses and the forums, and find it well worth the pennies.

You can always come to the forums for assistance ;).

Happy coding!

Jannes

1 Like

You are absolutely right, very embarrassing mistake there. I am deleting my frustrated and unhelpful comment. Thanks for the help!

1 Like

Anytime :wink: Happy coding!

I am getting the same error and based on the thread above I cannot figure out what is wrong, any hints?

Blockquote
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.mains = mains;
},
set desserts (desserts) {
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: function (courseName) {
const dishes = this._courses[courseName];
const randomNumber = Math.floor(Math.random * dishes.length);
return dishes[randomNumber];
},
generateRandomMeal: function() {
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’, ‘Caprese’, 11.50);
menu.addDishToCourse(‘appetizers’, ‘Dumplings’, 10.00);
menu.addDishToCourse(‘appetizers’, ‘French Onion Soup’, 5.25);
menu.addDishToCourse(‘appetizers’, ‘Cesar Salad’, 10.25);
menu.addDishToCourse(‘mains’,‘Prime Rib’, 55.00);
menu.addDishToCourse(‘mains’, ‘Eggplant Parm’, 25.00);
menu.addDishToCourse(‘mains’, ‘Seafood Tower’, 75.00);
menu.addDishToCourse(‘mains’, ‘Chicken Piccata’, 30.00);
menu.addDishToCourse(‘desserts’, ‘Panacotta’, 12.50);
menu.addDishToCourse(‘desserts’, ‘Chocolate Ice Cream’, 3.25);
menu.addDishToCourse(‘desserts’, ‘Cheese Plate’, 7.50);
menu.addDishToCourse(‘desserts’, ‘Cannoli’, 5.00);
const meal = menu.generateRandomMeal();
console.log(meal);

You should read the post I quoted below from this very thread.

wow, every time I looked at my code I said “yup, I’ve got the () there”, guess I just needed another set of eyes on it. Thank you so much.

so I added that () in Math.Random() and I am still getting the same error. Is there something else I am missing? I am new to this so I don’t really understand the error messages yet. Thank you.

Blockquote
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
const totalPrice = appetizer.dish.price + main.dish.price + dessert.dish.price;
^
TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48:36)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68:19)
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)

You must’ve changed that line of code at some point. You had it correct in the post I quoted.

I don’t know what the mistake was but I deleted the line and retyped it and now it is working!
Thank you so much for your help!

Hey, everyone.
Im getting the same error, below my code.
I try all the suggest that I find here, probably Im doing something wrong.

Any help?

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 = appetizerIn;
  },

  set mains(mainIn) {
    this._courses.mains = mainIn;
  },

  set desserts(dessertIn) {
    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;
  /*
  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},  ${dessert.name} and the price is $${totalPrice}.`;

  } 
};

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