Meal Maker - Code does not run, tried everything to fix it

On line 37 I run into this error: const randomIndex = Math.floor(Math.random() * dishes.length); TypeError: Cannot read property ‘length’ of undefined.

I’ve tried following the walkthrough but still no luck, I want to understand where I’ve gone wrong.

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

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.dessets,
    };
  },
  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('appetizers');
     const mains = this.getRandomDishFromCourse('mains');
     const desserts = this.getRandomDishFromCourse('dessers');
     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', 'Salad', 3.00);
menu.addDishToCourse('appetizers', 'Wings', 4.00);
menu.addDishToCourse('appetizers', 'Fries', 5.00);

menu.addDishToCourse('mains', 'Chicken Breasts', 13.00);
menu.addDishToCourse('mains', 'BBQ ribs', 9.00);
menu.addDishToCourse('mains', 'Tagine', 12.00);

menu.addDishToCourse('desserts', 'Chocolate Cake', 5.50);
menu.addDishToCourse('desserts', 'Rice Pudding', 5.50);
menu.addDishToCourse('desserts', 'Ice Cream', 5.50);

const meal = menu.generateRandomMeal();
console.log(meal);
getRandomDishFromCourse(courseName) {
    console.log(courseName)
    const dishes = this._courses[courseName];

You have a typo in your code. Add a console to your function getRandomDishFromCourse, see what you get and trace it back.

I get this:

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

TypeError: Cannot read property ‘length’ of undefined

This doesn’t look like a course…

1 Like

Wow, I’ve been awake for 18 hours trying to catch up on modules & thank you for pointing that out I realised my code was riddled with plural/singular typo errors, managed to correct all 10 of them and fix it, thank you once again!

1 Like