Meal maker Price

This is my first time posting so hopefully I do this right. I am struggling to understand how to get the price to work in the Meal Maker project.
Below is my code::

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(appetizer) {
    this._courses.appetizer = appetizer;
  },
  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,
    };
    return this._courses[courseName].push(dish.length);
    
  },

  getRandomDishFromCourse (courseName) {
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
  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 ${appetizers.name}, ${mains.name}, ${desserts.name}, and the total price is ${totalPrice}`;
  }
};

menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 5.00);
menu.addDishToCourse('appetizers', 'fries', 4.50);

menu.addDishToCourse('mains', 'burger', 11.00);
menu.addDishToCourse('mains', 'pasta', 10.00);
menu.addDishToCourse('mains', 'taco', 7.00);

menu.addDishToCourse('desserts', 'cake', 3.00);
menu.addDishToCourse('desserts', 'ice cream', 3.50);
menu.addDishToCourse('desserts', 'pie', 4.10);

let Meal = menu.generateRandomMeal();
console.log(meal);

And then the error I am receiving:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:50
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:50:36)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:67: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)

Please help :slight_smile:

Hello.

In the line return this._courses[courseName].push(dish.length); you add dish.length to the array instead of dish itself. And since the object doesn’t have a length property, you’ll end up with an undefined values in your _courses.

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