Meal Maker: dishes is undefined


#1

Hi everyone, yet another Meal Maker question. I looked up the questions asked about this exercise but I don’t seem to have the same problem. In my getRandomDishFromCourse I get undefined for my dishes variable.

  getRandomDishFromCourse (courseName) {
    let dishes = this._courses[courseName];
    let randomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },

When I run the code, I get “cannot read property length of undefined”.
I just don’t get why my dishes variable is not an array as it should be equal to menu._courses.appetizers or mains or dessert or whatever.

Here is the full code.

Thank you so much for your time!

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    dessert: []
  },
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      dessert: this._courses.dessert
    }
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizers) {
    this._courses.appetizers = appetizers;
  },
  get mains() {
    return this._courses.mains;    
  },
  set mains(mains) {
    this._courses.appetizers = mains;
  },
  get dessert() {
    return this._courses.dessert;    
  },
  set dessert(dessert) {
    this._courses.dessert = dessert;
  },
	addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };
    return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse (courseName) {
    let dishes = this._courses[courseName];
    let 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 + mains.price + dessert.price;
    
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert}.name. The price is ${totalPrice}$.`
  }
}

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'Shrimp Tostada', 6.50);
menu.addDishToCourse('appetizers', 'Sushis du chef', 7.00);
menu.addDishToCourse('mains', 'Lasagna', 13.75);
menu.addDishToCourse('mains', 'Mexican Burger', 14.50);
menu.addDishToCourse('mains', 'Asian Noodles', 12.25);
menu.addDishToCourse('dessert', 'Crme brle', 5.00);
menu.addDishToCourse('dessert', 'Jalapenos Brownies', 8.00);
menu.addDishToCourse('dessert', 'Fried Tempura Ice Cream', 6.00);

const meal = menu.generateRandomMeal();
console.log(meal);

#2

So I took a break and went back to it. Started from scratch and got it to work so I could compare with the code I wrote first time that did not work.

The problem was i wrote “desserts with an s” in my generateRandomMeal function

but wrote dessert without s when calling the addDishToCourse function

It’s what seemed to cause the problem. There was also a mistake with main.name in my return block that should have had an s (mains).

Glad I got this sorted. Good night!