Meal Maker Price is Undefined


#1

Here is my Code:

const menu = {
  _courses: {
    _appetizers:[],
    _mains :[],
    _desserts :[],
    
    get appetizers() {
      return this._appetizers;
    },
    set appetizers(appetizerIn){
      return this._appetizers.push(appetizerIn);
    },
    get mains() {
      return this._mains;
    },
    set mains(mainsIn) {
      return this._mains.push(mainsIn);
    },
    get desserts(){
      return this._desserts;
    },
    set desserts(dessertsIn){
      return this._desserts.push(dessertsIn);
    }
    
  },
  get courses(){
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
    }
  },
  addDishToCourse:function(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:function(){
    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", "Caviar", 6.35);
  menu.addDishToCourse("appetizers", "Papadums", 4.50);
  menu.addDishToCourse("appetizers", "Arab Salad", 7.20);
  menu.addDishToCourse("mains", "Sushi", 10.30);
  menu.addDishToCourse("mains", "Goat Curry", 12.00);
  menu.addDishToCourse("mains", "Spicy Noodles", 11.30);
  menu.addDishToCourse("desserts", "Chocolate Cake", 10.50);
  menu.addDishToCourse("appetizers", "Pączki", 9.75);
  menu.addDishToCourse("appetizers", "Sundae", 11.25);
let meal = menu.generateRandomMeal();
console.log(meal);

And here is the error that it returns:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:52
    const totalPrice = appetizer.price + main.price + dessert.price;
                                ^

TypeError: Cannot read property 'price' of undefined
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:52:33)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:66:17)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

#2

if we update this line:

const appetizer = this.getRandomDishFromCourse("appetizers");

to:

console.log(this.getRandomDishFromCourse("appetizers"));

we see that getRandomDishFromCourse returns undefined. you could use the same approach to figure out why getRandomDishFromCourse returns undefined


#3

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