Meal Maker - my code is not working

Link to exercise: Meal Maker URL
My code doesn’t work, please help:

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 (appetizerIn) {
    this._courses.appetizers = appetizerIn;
  set mains (mainIn) {
    this._courses.mains = mainIn;
  set desserts (dessertIn) {
    this._courses.desserts = dessertIn;
  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 `${} - ${} - ${}: Total is: ${totalPrice}.`;

menu.addDishToCourse('appetizers', 'Hummus', 15);
menu.addDishToCourse('appetizers', 'Aceitunas', 8);
menu.addDishToCourse('appetizers', 'Sopa', 5);

menu.addDishToCourse('mains', 'Falafel', 25);
menu.addDishToCourse('mains', 'Wrap', 23);
menu.addDishToCourse('mains', 'Burger', 28);

menu.addDishToCourse('dessert', 'Fresas', 7);
menu.addDishToCourse('dessert', 'Pastel chocolate', 6);
menu.addDishToCourse('dessert', 'Pay de limon', 5);

The error is:

return this._courses[courseName].push(dish);

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38:37)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:63:6)
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)

Hello, @danielbettech3797802.

Look closely at the error message. The .push() method is being called on an object that has not been defined. What is the object? this._courses[courseName], so that is where we start. Is _courses defined? Yes, and it has 3 properties that each have arrays as values. Everything is good so far. How about the value assigned to courseName? Where does it come from?

Using console.log(), you can make observations about what is happening. Try adding it as I’ve suggested above, and see if you can find the problem.

After you find your bug, consider the use of return in this line:

return this._courses[courseName].push(dish);

What is being returned, and to where?
Is it necessary?


Hey! Thanks for the console.log tip, I can see now that I was missing a letter ‘s’ when calling the addDishToCourse() in the desserts section.

Problem solved!

1 Like