Meal Maker - Error

Hey y’all,
I’ve been working on this problem for two hours now and can’t find out what I’m doing wrong. The code I’ve pasted below throws an error with the price object.

I’m not looking for someone to give me an answer, just looking to get pointed in the right direction. Not sure which part of my code is causing the console to throw this error. I’ve attached my code below. For now I’m moving on.

const menu = { _courses: { appetizers: [] , mains: [] , desserts: [] }, get appetizers() { return this._courses.appetizers; } , set appetizers(appetizersCourse) { this._courses.appetizers = appetizersCourse; } , get mains() { return this._courses.mains; } , set mains(mainsCourse) { this._courses.mains = mainsCourse; } , get desserts() { return this._courses.desserts } , set desserts(dessertsCourse) { this._courses.desserts = dessertsCourse; } , 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 }; this._courses[courseName].push(dish); } , getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName]; const dishesIndex = Math.floor(Math.random * dishes.length) return dishes[dishesIndex]; } , /// generateRandomMeal() { const appetizer = this.getRandomDishFromCourse('appetizers'); const main = this.getRandomDishFromCourse('mains'); const dessert = this.getRandomDishFromCourse('desserts'); const totalPrice = appetizer.price + main.price + main.dessert return `Your appetizer is ${appetizer.name}, your entree is ${main.name}, and your dessert is ${dessert.name} and your total comes out to ${this.generateRandomMeal().totalPrice}.`; } }; menu.addDishToCourse('appetizers', 'asparagus', 12) menu.addDishToCourse('appetizers', 'gyoza', 8) menu.addDishToCourse('appetizers', 'nachos', 10) menu.addDishToCourse('mains', 'steak', 58) menu.addDishToCourse('mains', 'fish', 38) menu.addDishToCourse('mains', 'burger' , 24) menu.addDishToCourse('desserts', 'cake', 18) menu.addDishToCourse('desserts', 'chocolate', 4) menu.addDishToCourse('desserts', 'ice cream', 16) const meal = menu.generateRandomMeal(); console.log(meal)

Thanks for the help!

Welcome to the forums!

In the function getRandomDishFromCourse
on this line:
const dishesIndex = Math.floor(Math.random * dishes.length)

Math.random should be Math.random()

in the function generateRandomMeal
on this line:

return `Your appetizer is ${appetizer.name}, your entree is ${main.name}, and your dessert is ${dessert.name} and your total comes out to ${this.generateRandomMeal().totalPrice}.`;

this.generateRandomMeal().totalPrice
should be
totalPrice
otherwise, the function tries to call itself (which is recursion)
but it fails because it tries to do that over and over without getting something else to return.
(and the error says “Maximum call stack size exceeded” or “stack overflow”).

Also in that function:
in the line
const totalPrice = appetizer.price + main.price + main.dessert
the last one should be
dessert.price
instead of
main.dessert

1 Like

Thank you so much! This was my fourth time trying to do the problem and the second two issues seemed to stem from me being so tired. I can’t believe I wasted so much time on this problem because of the parentheses. Once again thanks a lot!