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!