Why do I keep getting Type Error in this problem?

Hello, I was following along the code but I keep getting this error message "maker/app.js:39
return this._courses[courseName].push(dish);
^

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse"**

Can someone explain what I’m doing wrong here?
I keep looking through the code but I can’t figure out why the error keeps popping up!!! i think the error is in this function " addDishToCourse(courseName, dishName, dishPrice){
const dish = {
name: dishName,
price:dishPrice,
};
//property assignment first, then push the dish object into _courses
return this._courses[courseName].push(dish);
},"

THE COSDE IS BELLOW

const menu = {
//Create three properties inside the _courses object called appetizers, mains, and desserts. Each one of these should initialize to an empty array
_courses: {
appetizers: ,
mains: ,
desserts: ,
},
get appetizers(){
return this._courses.appetizers;
},
get mains(){
return this._courses.mains;
},
get desserts(){
return this._courses.desserts;
},
set appetizers(parameterOfAppetizers){
this._courses.appetizers= parameterOfAppetizers;
},
set mains(parameterOfMains){
this._courses.mains = parameterOfMains;
},
set desserts(parameterOfDesserts){
this._courses.desserts= parameterOfDesserts;
},
get courses(){
return{
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
};
},
addDishToCourse(courseName, dishName, dishPrice){

  •  const dish = {*
    
  •    name: dishName,*
    
  •    price:dishPrice,*
    
  •  };*
    
  •  //property assignment first, then push the dish object into _courses*
    
  • return this._courses[courseName].push(dish);*
    
  • },*
    // create a function that allow to get a random dish from a course on the menu, which will be necessary for generating a random meal.
    getRandomDishFromCourse(courseName){
    //Retrieve the array of the given course’s dishes from the menu‘s _courses
    const dishes = this._courses[courseName];
    const randomIndex =Math.floor(Math.random()* dishes.length);
    return dishes[randomIndex];
    },
    //generate a three course meal
    generateRandomMeal(){
    const appetizer= this.getRandomDishFromCourse(‘appetizers’);
    const main= this.getRandomDishFromCourse(‘mains’);
    const dessert= this.getRandomDishFromCourse(‘desserts’);
    //calculate total price
    const totalprice = appetizer.price + main.price + dessert.price;
    return Your meal is ${appetizer.price}, ${main.price}, and ${dessert.price}, and the total price is ${totalprice} ;
    }
    };
    //create the menu
    menu.addDishToCourse(‘Appetizer’,“Salad”, 4);
    menu.addDishToCourse(‘Appetizer’,“Soup”, 5.50);
    menu.addDishToCourse(‘Appetizer’,“Fries”, 5);

menu.addDishToCourse(‘Mains’,“Salmon”, 10);
menu.addDishToCourse(‘Mains’,“Steak”, 14);
menu.addDishToCourse(‘Mains’,“Pizza”, 12);

menu.addDishToCourse(‘Desserts’,“Fruit_salad”, 4);
menu.addDishToCourse(‘Desserts’,“Ice_cream”, 5);
menu.addDishToCourse(‘Desserts’,“Frappe”, 4.50);
//generate random meal
const meal = menu.generateRandomMeal();
console.log(meal);

We don’t need or want to return from a .push() statement unless the new length of the array is important and expected. Drop return from that statement.

Check that you are assigning to an array object. That may mean stepping back from all the methods (comment out the calls) and testing the menu.courses.appetizers after adding one. Add a mains and a desserts, and test, as well.

Thank you

The problem was in the

when I was first running it, I had,
" * menu.addDishToCourse(‘Appetizer’,“Salad”, 4);
menu.addDishToCourse(‘Appetizer’,“Soup”, 5.50);
menu.addDishToCourse(‘Appetizer’,“Fries”, 5);

menu.addDishToCourse(‘Mains’,“Salmon”, 10);
menu.addDishToCourse(‘Mains’,“Steak”, 14);
menu.addDishToCourse(‘Mains’,“Pizza”, 12);

menu.addDishToCourse(‘Desserts’,“Fruit_salad”, 4);
menu.addDishToCourse(‘Desserts’,“Ice_cream”, 5);
menu.addDishToCourse(‘Desserts’,“Frappe”, 4.50);"

after changing it to
“” //create the menu
menu.addDishToCourse(‘appetizers’,“Salad”, 4);
menu.addDishToCourse(‘appetizers’,“Soup”, 5.50);
menu.addDishToCourse(‘appetizers’,“Fries”, 5);

menu.addDishToCourse(‘mains’,“Salmon”, 10);
menu.addDishToCourse(‘mains’,“Steak”, 14);
menu.addDishToCourse(‘mains’,“Pizza”, 12);

menu.addDishToCourse(‘desserts’,“Fruit_salad”, 4);
menu.addDishToCourse(‘desserts’,“Ice_cream”, 5);
menu.addDishToCourse(‘desserts’,“Frappe”, 4.50);
"
I guess I wasn’t calling the arrays correctly

1 Like