Stuck on MealMaker

Hey All,

Please help me figure out the problem in my code, I have gone back to back comapring it to many many working codes out there, yet I fail to notice my mistake. This is my code:

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(mainsIn){
 this._courses.mains = mainsIn;

  },

    set desserts(dessertsIn){

    this._courses.desserts = dessertsIn;

  },

  get courses() {

    return {

      appetizers: this.appetizerIn,

      mains: this.mainsIn,

      desserts: this.dessertsIn

    };

  },

 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(){

    const appetizers = this.getRandomDishFromCourse(this._courses.appetizers)

    const mains = this.getRandomDishFromCourse(this._courses.mains)

    const desserts = this.getRandomDishFromCourse(this._courses.desserts)

    const totalPrice = appetizers.price + mains.price + desserts.price;

    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name} and the total price is: ${totalPrice}`

  },

};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);

menu.addDishToCourse('appetizers', 'tuna', 1.25);

menu.addDishToCourse('mains', 'Steak', 10.25);

menu.addDishToCourse('main', 'Cheese Burger', 8.25);

menu.addDishToCourse('desserts', 'tiramisu', 2.25);

menu.addDishToCourse('desserts', 'Pancake', 5.25);

const meal = menu.generateRandomMeal;

And the error message is the following:
TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:30)

Please help me so I can sleep :smiley:

Hello, @script4959091261, and welcome to the forums.

The error message is telling you that you are attempting to use the .push() method on something that doesn’t exist (undefined) . The easiest way to see what is going on, is to print the values in question out to the console before and after acting upon them. Try logging the undefined value to the console, and see what you get.

Thanks for the reply!

Now I get this: appetizers
[ { name: ‘Caesar Salad’, price: 4.25 } ]
appetizers
[ { name: ‘Caesar Salad’, price: 4.25 },
{ name: ‘tuna’, price: 1.25 } ]
mains
[ { name: ‘Steak’, price: 10.25 } ]
mains
[ { name: ‘Steak’, price: 10.25 },
{ name: ‘Cheese Burger’, price: 8.25 } ]
desserts
[ { name: ‘tiramisu’, price: 2.25 } ]
desserts
[ { name: ‘tiramisu’, price: 2.25 },
{ name: ‘Pancake’, price: 5.25 } ]
[Function: generateRandomMeal]

And no idea why it wont push them or about what is going on exactly :-/

Not sure how you got that output. Running the code you shared with the console.log() statements added I get this:

appetizers
[]
appetizers
[ { name: 'Caesar Salad', price: 4.25 } ]
mains
[]
main
undefined

If you changed your code in some way, you’ll need to post the updated version, so we’re on the same page. Make sure you post your code following the guidelines here: How do I format code in my posts?

I edited your original post to format the code for you.

Thanks for you reply, right now this is my code:
I tried to ctr v your console.log reply into my code but it wouldn’t print anything so I put it into the palce where it is right now.

 _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(mainsIn){
   this._courses.mains = mainsIn;
 },
   set desserts(dessertsIn){
   this._courses.desserts = dessertsIn;
 },
 get courses() {
   return {
     appetizers: this.appetizerIn,
     mains: this.mainsIn,
     desserts: this.dessertsIn
   };
 },
addDishToCourse(courseName, dishName, dishPrice) {
   const dish = {
     name: dishName,
     price: dishPrice
   };
   this._courses[courseName].push(dish);
   console.log(courseName);
   console.log(this._courses[courseName]);
},
 getRandomDishFromCourse: function (courseName){
   const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length)
   return dishes[randomIndex];
 },
 generateRandomMeal(){
   const appetizers = this.getRandomDishFromCourse(this._courses.appetizers)
   const mains = this.getRandomDishFromCourse(this._courses.mains)
   const desserts = this.getRandomDishFromCourse(this._courses.desserts)
   const totalPrice = appetizers.price + mains.price + desserts.price;
   return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name} and the total price is: ${totalPrice}`
 },
};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'tuna', 1.25);
menu.addDishToCourse('mains', 'Steak', 10.25);
menu.addDishToCourse('mains', 'Cheese Burger', 8.25);
menu.addDishToCourse('desserts', 'tiramisu', 2.25);
menu.addDishToCourse('desserts', 'Pancake', 5.25);


const meal = menu.generateRandomMeal;

console.log(meal)```

Okay. It looks like you fixed the error you originally had by changing main to mains here.

So, what is the next issue?

2 Likes