Meal Maker: TypeError at totalPrice calc

I’ve been stuck on this meal maker project for over a week. I’ve followed right along with the project walkthrough so my code is nearly identical to it. I’ve checked for typos, syntax errors, I’m at a loss. Here is the link to the project: Meal Maker Project

Here is my 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(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(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 chosen meal is ${appetizer.name} and ${main.name} with ${dessert.name} for dessert. It will cost \$${totalPrice}. Enjoy! `
  }
};
menu.addDishToCourse('appetizers', 'Mozzarella Sticks', 6.50);
menu.addDishToCourse('appetizers', 'Calamari', 8.50);
menu.addDishToCourse('appetizers', 'Stuffed Mushrooms', 7.00);
menu.addDishToCourse('appetizers', 'Nacho Platter', 5.00);

menu.addDishToCourse('mains', 'Spaghetti and Meatballs', 10.50);
menu.addDishToCourse('mains', 'Chicken Parmesan', 12.00);
menu.addDishToCourse('mains', 'Grilled Balsamic Salmon', 15.00);
menu.addDishToCourse('mains', 'Lamb Tikka Masala', 14.00);

menu.addDishToCourse('desserts', 'Molten Lava Cake', 4.50);
menu.addDishToCourse('desserts', 'Mango Gelato', 5.50);
menu.addDishToCourse('desserts', 'New York Cheesecake', 6.50);


const meal = menu.generateRandomMeal();

console.log(meal)

Here is my error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
    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:48:34)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:67: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)

Whaaaaattttt is wrong here :weary:

Each course (appetizers, mains, desserts) is an array. You can’t access items in an array by using dot notation.

Your Math.random function is missing parentheses, which messes up your randomIndex variable.

1 Like

That was it! Thank you!!

1 Like

Please help:
I have an error message:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
const totalPrice = appetizer.price + mains.price + desserts.price;
^

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

Here is my 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(ppetizers) {
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(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 + mains.price + desserts.price;
return Your meal is ${appetizer.name}, ${main.name} and ${dessert.name}, and the total price is ${totalPrice};
}
};

menu.addDishToCourse(‘appetizers’, ‘salad’, 4.00);
menu.addDishToCourse(‘appetizers’, ‘natto’, 1.00);
menu.addDishToCourse(‘appetizers’, ‘egg’, 4.00);

menu.addDishToCourse(‘mains’, ‘tofu’, 6.25);
menu.addDishToCourse(‘mains’, ‘nigiri’, 4.50);
menu.addDishToCourse(‘mains’, ‘miso soup’, 2.00);

menu.addDishToCourse(‘desserts’, ‘matcha’, 5.50);
menu.addDishToCourse(‘desserts’, ‘osembe’, 4.00);
menu.addDishToCourse(‘desserts’, ‘peanuts’, 4.00);

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

Thank you!

You did the same thing as me. You need empty () after Math.random

Also, it looks like you have a typo on your appetizer setter. it says appetizers(“ppetizers”)

Thank you so much!!!

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