JavaScript Meal Maker Project Type Error

Hi everyone I have problem with JS Meal Maker Project. I wrote the same code as shown on Video walkthrough but I still getting Type error:

return this._courses[courseName].push(dish);
TypeError: Cannot read property ‘push’ of undefined

My code shown below:

const menu = 
{
  _courses:
  {
    appetizers: [],
    mains: [],
    desserts: []
  },
  
//-----------------------------------
  
  get appetizers()
  {
    return this._courses.appetaizers;
  },
  get mains()
  {
    return this._courses.mains;
  },
  get desserts()
  {
    this._courses.desserts;
  },
  
//-----------------------------------
  
  set appetizers(appetizers)
  {
    this._courses.appetizers = appetizers;
  },
  set mains(mains)
  {
    this._courses.mains = mains;
  },
  set desserts(desserts)
  {
    this._courses.deserts = desserts;
  },
  
//-----------------------------------
  
  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);** // <=== ERROR HERE
  },
  
//--------------------------------------
  
  getRandomDishFromeCourse(courseName)
  {
    const dishes =
    this._courses[courseName];
    const randomIndex =
    Math.floor(Math.random()*dishes.length);
    return dishes[randomIndex];
  },
  
//---------------------------------------
  
  generateRandomMeal()
  {
    const apetizer = 
    this.getRandomDishFromeCourse('appetizers');
    const main =
    this.getRandomDishFromeCourse('mains');
    const dessert =
    this.getRandomDishFromeCourse('desserts');
    
    const totalPrice = appetizer.price + main.price  + dessert.price;
    
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}.
    The price is $${totalPrice}.`;
  }
};

//------------------------------------------
menu.addDishToCourse('apetaizers', 'Pizza', 10);
menu.addDishToCourse('apetaizers', 'Pasta', 10);
menu.addDishToCourse('apetaizers', 'Pojo', 10);

menu.addDishToCourse('meins', 'Pierogi', 5);
menu.addDishToCourse('mains', 'Schabowy', 10);
menu.addDishToCourse('meins', 'Pomidorowa', 5);
 
menu.addDishToCourse('desserts', 'Losos', 12);
menu.addDishToCourse('desserts', 'Kraby', 15);
menu.addDishToCourse('desserts', 'Malze', 10);

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

And one more time errot type mesage as a pic this time

Please help me. Thank you.

Hello, @howtoco.

Welcome to the forums.

Please review this post: How do I format code in my posts? Following the guidelines there will make you code appear as it does now when you post it in the future. (I edited your post.) With just a brief glance at you code, the first thing that stands out is spelling inconsistencies. I’d fix those first, and see what I have.

1 Like

Ok thank you for yours tip.
Can you help me fix this problem?
I did reviev the post.

Look at your property names:

const menu = 
{
  _courses:
  {
    appetizers: [],
    mains: [],
    desserts: []
  },

You have appetizers, mains & desserts. In your code, I see appetaizers & apetaizers, I see deserts, I see meins. If you want to push values to or retrieve values from an array named mains, you can’t refer to it using meins. You need to carefully read your code, and fix the spelling errors.

1 Like

OK I see now and I fixed those.
Thank you for help.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.