Meal Maker - I'm having problems!

Hey everyone! I’m currently re-doing the Meal Maker project and I’m kind stuck at one part. I keep getting this error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
   this._courses[courseName].push(dish);
                            ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse

I haven’t finished the whole thing but here’s my code so far:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },
  
  get appetizers() {
    return this._appetizers
  },
  
  get mains() {
    return this._mains
  },
  
  get desserts() {
    return this._desserts
  },
  
  set appetizers(appetizers) {},
  set mains(mains) {},
  set desserts(desserts) {},
  
  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: function(courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
  
  generateRandomMeal: function() {
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Tonight you will be having ${appetizer.name}, ${main.name}, and ${dessert.name} for dessert. The total price will come out to ${totalPrice}.`
  },
};

menu.addDishToCourse('appetizer', 'salad', 4.99);
menu.addDishToCourse('appetizer', 'boneless wings', 4.99);
menu.addDishToCourse('appetizer', 'salad', 4.99);

Someone please help! I’ve done this project already but I just wanted to get a little more practice. I’m confused because I did it before without an issue! What did I do wrong here? Thanks in advance for any help!

For the error you currently have the fix is relatively simple. Look at the name of the first property of your _courses object: appetizers.
Now look at your calls to the addDishToCourse method:

menu.addDishToCourse('appetizer', 'salad', 4.99);
menu.addDishToCourse('appetizer', 'boneless wings', 4.99);
menu.addDishToCourse('appetizer', 'salad', 4.99);

Using appetizer instead of appetizers is causing your error. :wink:
You will have a problem later with your getRandomDishFromCourse & generateRandomMeal functions. The syntax you used is incorrect. You’ll need one of these options:

function myFunctionName(parameters) {
  //code goes here
}

OR

const myFunctionName = (parameters) => {
  //code goes here
}

Hope this helps! && Happy Coding!

3 Likes

Omg thanks! I’ll try again and see if it works out.
Also for the functions, that was the syntax used in the hint?? But it’s okay I’ll do this instead. Thank you!

Hold on. It was late, and my brain was apparently tired. I’m also fairly new to JavaScript, and didn’t look at the hint until now. That syntax will work (I just tried it). Also the examples I gave are correct syntax for functions, but inside the menu object we are actually creating methods. The examples I gave won’t work inside the menu object. My apologies!
You can however do this:

generateRandomMeal() {
  //code goes here
}

instead of:

generateRandomMeal: function() {
  //code goes here
} // a little quick research seems to indicate this is older ES5 syntax
  //it actually adds generateRandomMeal as a property of the menu object
 //and assigns the function as its value
//no longer necessary with ES6, but still works

but both will work.
Again, sorry for the confusion.