Meal Maker bugs!

const menu = {
    _courses: {
      appetizers: [],
      mains: [],
      desserts: []
    },
    get appetizers() {
        return this._courses.appetizers;
      },
    set appetizers(appetizersIn) {
        this._courses.appetizers = appetizersIn
      },
    get mains() {
        return this._courses.mains;
      },
    set mains(mainsIn) {
        this._courses.mains = mainsIn;
      },
    get desserts() {
        return this._courses.mains;
      },
    set desserts(dessertsIn) {
        this._courses.desserts = dessertsIn
      },
    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);
    },
    getRandomDishFromCourse(courseName) {
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * dishes.lenght);
      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 ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}.`;
    },
  }
  
  menu.addDishToCourse('appetizers', 'Chicken Skewers', 6.99)
  menu.addDishToCourse('appetizers', 'Spinach Dip', 5.00)
  menu.addDishToCourse('appetizers', 'Fried Calamari', 11.99)

  menu.addDishToCourse('mains', 'Chicken Marsala', 16.99)
  menu.addDishToCourse('mains', 'Chicken Parmesan', 15.99)
  menu.addDishToCourse('mains', 'Ribeye Steak', 24.99)
  
  menu.addDishToCourse('desserts', 'Carrot Cake', 7.99)
  menu.addDishToCourse('desserts', 'Tres Leches', 7.99)
  menu.addDishToCourse('desserts', 'Chocolate Mouse', 7.99)
  
  const meal = menu.generateRandomMeal();
  console.log(meal)

The error:

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

TypeError: Cannot read property 'price' of undefined

Could someone kindly go over this and help me find what my error is? I’ve been at it for a while and can’t figure out why I get this error.

Since the error message tells us that appetizer is undefined, and the value for that was set with the call this.getRandomDishFromCourse('appetizers'), then we need to check the code for the getRandomDishFromCourse() method.

If you add some logging to that method, you’ll find that randomIndex doesn’t get set to the value you expect. You can focus your efforts on that line of code to uncover the issue

Click here if you need another hint

dishes.lenght

1 Like

:man_facepalming:
I’ll never misspell that word again!

Thank you so much!

1 Like