JAVASCRIPT SYNTAX, PART II Meal Maker

Code

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizersIn){
    this._courses.appetizers = appetizersIn;
  },
  get mains() {
    return this._courses.mains;
  },
  set mains(mainsIn){
    this._courses.mains = mainsIn;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(dessertsIn){
    this._courses.desserts = dessertsIn;
  },
  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.getRandomFromCourse("appetizers");
    const menu = this.getRandomFromCourse("menus");
    const dessert = this.getRandomFromCourse("desserts");
    const totalPrice = appetizer.price + menu.price + dessert.price;
    return `This dishes which have generated randomly are ${appetizer.name}, ${menun.name} and ${dessert.name}. The total amount which you have to pay is ${totalPrice} Ruppes`;
  }
};
menu.addDishToCourse('appetizers','Cutlet',180);
menu.addDishToCourse('appetizers','Fries',140);
menu.addDishToCourse('appetizers','Salad',190);
menu.addDishToCourse('menus','roti and panner',240);
menu.addDishToCourse('menus','naan and panner',280);
menu.addDishToCourse('menus','roti and aloo',185);
menu.addDishToCourse('desserts','moose',80);
menu.addDishToCourse('desserts','ice-cream',100);
menu.addDishToCourse('desserts','Brownie',130);

const meal = menu.generateRandomMeal();
console.log(meal);

Error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
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:37:37)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:55: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)

Please help me with this error
Thank You

Please help me with this.
Thank You

The first obvious step would to inspect the state of our program, we can use .log() for this:

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    console.log(Object.keys(this._courses), courseName);
    return this._courses[courseName].push(dish);
  },

After inserting the .log() into your code and running the code, you should see you attempt to use a key ("menus") which does not exists on the courses object

I personally think JavaScript could have given a better error, now it fails silently resulting a more difficult to debug bug later.

Ok Thanks
If you come up with the solution please let me know.
Thank You

I have done this project couple of times, helped a dozen more people. I can dream this project.

So what? You want me to provide you with the solution?

Yes Please provide me with the solution, I just want to see where I’m getting wrong or instead you can point out the problem in my code.
Thank You

I don’t give away solutions, if you want a solution, you can easily find them online.

I showed you where your problem is? you try to add menus as a course:

menu.addDishToCourse('menus','roti and panner',240);
menu.addDishToCourse('menus','naan and panner',280);
menu.addDishToCourse('menus','roti and aloo',185);

while this is not one of the available courses

Hi ajax2222383535,
I did acctually read all of your code, You acctually have syntax error in couple of places, try
to debug them your self, it even tells you the line number. Debugging is very important.