Meal Maker: Cannot read property 'length' of undefined

For some reason i get Cannot read property ‘length’ of undefined
i tried a little to find out where the problem can be solved, but can’t find it.

this 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(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,
    };
    return this._courses[courseName].push(dish);
  },
  
  getRandomDishFromCourse (courseName) {
   const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length);
   return dishes[randomIndex];
  },
  
  generateRandomMeal () {
    const appetizer = this.getRandomDishFromCourse('appetizer');
    const main = this.getRandomDishFromCourse('main');
    const dessert = this.getRandomDishFromCourse('dessert');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}, and the total price is ${totalPrice}.`;
  }
};
menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'beef', 12.75);
menu.addDishToCourse('mains', 'sparerib', 11.50);
menu.addDishToCourse('mains', 'pork', 11.50);

menu.addDishToCourse('desserts', 'Irish Coffe', 7.50);
menu.addDishToCourse('desserts', 'creme brullee', 6.00);
menu.addDishToCourse('desserts', 'ice cream', 5.00);

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

and this is the error i get

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:44
const randomIndex = Math.floor(Math.random() * dishes.length);
^

TypeError: Cannot read property ‘length’ of undefined
at Object.getRandomDishFromCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:44:57)
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:49:28)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68:19)
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)

can someone help me?

Thanks.

3 Likes

i’ve found the problem at: generateRadomMeal i missed the letter S after appetizer, main and dessert.

now my code is working :stuck_out_tongue:

8 Likes

Thanks for sharing! I was also stuck at the same problem ahahah

3 Likes

I went through an error three times on this project. Exact same problem I’m dead

1 Like

this worked for me ! thanks.

Lol same issue I had, I kept spelling ‘appetizers’ differently, it would be either appertizer or appetizers