Meal Maker project error when running

Hi,

Can someone please help with my code below, if I feel like its got loads of errors in it, but I’m currently getting the following error.
TypeError: Cannot read property ‘price’ of undefined

const menu={

  _courses:{

    _appetizers:[],

    _mains:[],

    _desserts:[],

    get appetizers(){

      return this._appetizers;

    },

    set appetizers(appetizers){

      this._appetizers=appetizers

    },

    get mains(){

      return  this._mains;

    },

    set mains(mains){

      this._mains=mains;

    },

    get desserts(){

      return this._desserts;

    },

    set desserts(desserts){

      this._desserts=desserts;

    },

  },

  get course()

  {

    return{

      appetizers:this._courses.appetizers,

      main:this._courses.mains,

      desserts:this._courses.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];

  },

  getRandomFood()

  {

    const appetizer = this.getRandomDishFromCourse('appetizers')

    const main = this.getRandomDishFromCourse('mains')

    const dessert = this.getRandomDishFromCourse('desserts')

    const totalCost = appetizer.price + main.price + dessert.price

    return 'Your meal is ${appetizer.name} , ${main.name} and ${dessert.name}. The total price incurred is $${totalCost}';

  },

};

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

menu.addDishToCourse('appetizers', 'Prawn Coctail', 4.25);

menu.addDishToCourse('appetizers', 'Garlic Bread', 3.50);

menu.addDishToCourse('mains', 'Lasagna', 9.75);

menu.addDishToCourse('mains', 'Ribeye Steak', 14.95);

menu.addDishToCourse('mains', 'Fish & Chips', 12.95);

menu.addDishToCourse('desserts', 'Cheese Cake', 4.50);

menu.addDishToCourse('desserts', 'Creme Brule', 4.25);

menu.addDishToCourse('desserts', 'Cheese Board', 3.25);

let meal = menu.getRandomFood();

console.log(meal);

then, what did you try to read the property price from, and, where did you get that from?

similar scenario:

let x = {}
x.y.price // error

Your error message is telling you that you’ve got nothing, and you’re trying to obtain price from nothing.

1 Like

on get courses, change this:

appetizers:this.appetizers,

  main:this.mains,

  desserts:this.desserts,
2 Likes

Really appreciate your time on this , but tried this to no luck :frowning: . I think the problem lies somewhere in accessing the object properties , then again im just a sore n00b .

Thanks for your time on this , so i get that the price property that the dish object returns has to be passed down to _courses and subsequently appetizers , mains and desserts respectively . I just cant seem to put my finger on it . I’m pretty sure this has got to do with accessing the _courses and pushing the dish object down the array.

Really appreciate again for your time on this.

That is the line that is throwing the error correct? So lets work backwards from there since everything seemed to work until that line. console.log() will very much be your friend when debugging code so lets start with just logging appetizer just before that line. If appetizer is undefined that tells you that something is wrong with this.getRandomDishFromCourse('appetizers'). Is it a typo? Probably not since that would throw a syntax error. Maybe the bug is inside getRandomDishFromCourse()? Why don’t you try adding some console.log()s inside that method. Can you find the bug?

Thanks a ton , i managed to solve the issue refacoring the whole thing from the get-go and honestly i think the problem is pretty dumb :grimacing: :joy:

all this seems to have been caused from the incorrect syntax at

supposedly must have caused the whole thing to snowball into some demonic error of sorts.

Thanks for all of your efforts ,

1 Like

A lot of your bugs are going to be like that but always in slightly different ways. Learning to debug is just as important as learning to code.

1 Like