Meal Maker Error: Maximum call stack size exceeded

Hi,

I have followed the Meal Maker project in Intro to Javascript for objects and double checked the video it comes with but I get an error: Maximum call stack size exceeded. I am pretty sure I have the same as the video now and I still get an error, see my code below -

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 {                           **THIS IS WHERE I HAVE THE ERROR**
      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);    
  },
  getRansomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
  generateRandomMeal() {
    const chosenAppetizer = this.getRanomDishFromCourse(appetizers);
    const chosenMain = this.getRanomDishFromCourse(mains);
    const chosenDessert = this.getRanomDishFromCourse(desserts);
    const totPrice = chosenAppetizer.price + chosenMain.price + chosenDessert.price;
    return `Appetizer is ${chosenAppetizer.name}, Main is ${chosenMain.name}, Dessert is ${chosenDessert.name} and the total cost is: ${totPrice}`;
  }
};


menu.addDishToCourse('appetizers', 'Tomato Soup', 4.50);
//menu.addDishToCourse('appetizers', 'Chicken Pate', 5.50);
//menu.addDishToCourse('appetizers', 'Salmon', 6.50);
//menu.addDishToCourse('mains', 'Chicken Burger', 12.50);
//menu.addDishToCourse('mains', 'Pesto Pasta', 10.50);
//menu.addDishToCourse('mains', 'Pizza', 11.50);
//menu.addDishToCourse('desserts', 'chocolate cake', 4.50);
//menu.addDishToCourse('desserts', 'ice cream', 3.50);
//menu.addDishToCourse('desserts', 'Fruit salad', 4.50);

//console.log(menu.generateRandomMeal());

Can anyone help?

Thanks,
Avril

Hi there mate, double check line 25. There’s something there that shouldn’t be there. _

You will then find more errors on line 45,46 and 47 let me know if you can’t find the issue. Also, double check your spelling on the word Random and make sure you got all the necessary ’ ’ .

Ah, thanks… I have corrected the lines you suggested and no more errors on that but now I get an error on the generateRandomMeal(), see below -

My code
generateRandomMeal() {
let appetizer = this.getRandomDishFromCourse(‘appetizers’);
let main = this.getRandomDishFromCourse(‘mains’);
let dessert = this.getRandomDishFromCourse(‘desserts’);
const totPrice = appetizer.price + main.price + dessert.price;
return Your meal is ${appetizer}, ${main}, ${dessert} and the total cost is: ${totPrice};
}

The error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
const totPrice = 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:31)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:64:18)
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 more I look at it I can see the issue is actually the below code…

addDishToCourse(courseName, dishName, dishPrice) {
let dish = {
name: dishName, // this is undefined
price: dishPrice, // this is undefined
}
this._courses[courseName].push(dish);
},

So I am thinking dish is not being reached properly, am I close?

I can see lot’s of people have been issues with this and I personally feel that the video that provides a solution is miss leading as he did it fine but when we all try it the same it doesn’t work. I understand there is something that I am missing but after trying for so many days I then compared the code in the video and it looks the same to me.

I really need some help please.

Thanks,
Avril

1 Like

Well I copy and past your first code and just changed those lines I told you and it worked!

I can see that your made changes from:

generateRandomMeal() {
const chosenAppetizer = this.getRanomDishFromCourse(appetizers);
const chosenMain = this.getRanomDishFromCourse(mains);
const chosenDessert = this.getRanomDishFromCourse(desserts);
const totPrice = chosenAppetizer.price + chosenMain.price + chosenDessert.price;
return Appetizer is ${chosenAppetizer.name}, Main is ${chosenMain.name}, Dessert is ${chosenDessert.name} and the total cost is: ${totPrice};
}
};

to:
generateRandomMeal() {
let appetizer = this.getRandomDishFromCourse(‘appetizers’);
let main = this.getRandomDishFromCourse(‘mains’);
let dessert = this.getRandomDishFromCourse(‘desserts’);
const totPrice = appetizer.price + main.price + dessert.price;
return Your meal is ${appetizer}, ${main}, ${dessert} and the total cost is: ${totPrice} ;
}

not sure why you changed this?

The whole code is with const!!!

Thanks for your help (not sure I needed all the exclamation marks though). I was still getting errors so I was playing about with it… was hard for me. I see now though.

Thanks,
A

1 Like

I didn’t even realise I put so many exclamations marks, sorry is just bad habit really. Yeah I had a super difficult time with this one and to be honest, even had the same mistake of line 25.