Meal Maker : need help!

Hi There

I’m stuck with this project in java exercise “Meal Make” it shown error “TypeError: Cannot read property ‘price’ of undefined”

here is a link of code : https://gist.github.com/a0684358b4ed36901d659891c94b712d

so could you guy help me to find out where is error?

Thanks in advance!

-------------
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(appetizers){
    this._courses.appetizers = appetizers;
  },
  set mains(mains){
    this._courses.mains = mains;
  },
  set desserts(desserts){
    this._courses.desserts = desserts;
  },  
  
  get courses(){
    return{
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    }
  },
  
  addDishToCourse(courseName,dishName,dishPrice){
    const dish = {
      name:dishName,
      price:dishPrice,
    };
    this._courses[courseName].push(dish);
  },
 getRandomDishFromCourse(courseName) {
     const dishes = this._courses[courseName];
     const randomNumber = Math.floor(Math.random() * dishes.length);
     return dishes[randomNumber];
},
  
generateRandomMeal(){
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('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('appetizers','Caesar Salad', 4.25);

let meal = menu.generateRandomMeal();

console.log(meal);

And yes it will run an error because you have not added any mains or desserts.

Ohh i get it. this is lesson to learn for me. Thank you very much :slight_smile:

1 Like

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