Meal Maker question - price is undefined

const menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: [],
    set appetizers(newAppetizers) {
    	this._appetizers = newAppetizers;
    get appetizers() {
    	return this._appetizers;
    set mains(newMains) {
      this._mains = newMains;
    get mains() {
      return this._mains;
    set desserts(newDesserts) {
      this._desserts = newDesserts;
    get desserts() {
      return this._desserts;
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
  addDishToCourse(courseName,dishName,dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
  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 ${}, ${}, and ${}. The price is ${totalPrice}.`;

menu.addDishToCourse('mains', 'pizza', 5.00);
menu.addDishToCourse('mains', 'sandwich', 4.00);
menu.addDishToCourse('mains', 'nachos', 6.00);

menu.addDishToCourse('appetizers', 'chips', 2.00);
menu.addDishToCourse('appetizers', 'veggies', 3.00);
menu.addDishToCourse('appetizers', 'onion rings', 3.00);

menu.addDishToCourse('desserts', 'cake', 2.00);
menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'fruit', 4.00);

let meal = menu.generateRandomMeal();


At line 50, where it says const totalPrice = appetizer.price + main.price + dessert.price; , the error I receive is “Cannot read property ‘price’ of undefined” and I cannot for the life of me figure out why. I thought price was defined when adding it via the addToDishCourse() method.

I would love any advice on this error.

I was not able to check further for any more potential errors, so there most likely will be more. No need to correct every error I have - I will try on my own. But for now, I cannot figure out why this error is showing up.



There is your culprit.

Math.random() * ...


Wow thank you! Lesson learned.

Also, why did the error point me to price, instead of the incorrectly used Math.random() function? How did you find this error? I want to learn for next time! :grin:

Thanks again!


Because that’s when the error was thrown. Looking backwards from that point for where the variable is defined is how I spotted the error.


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