Meal maker help

Hello, i am dealing with meal Maker challenge from js, i’ve tried to correct my code but i keep getting this " const randomIndex = Math.floor(Math.random() * dishes.length) TypeError: Cannot read property ‘length’ of undefined", apparently dishes is empty but i don’t understand why

I have followed the walkthrought but i can’t find where i am wrong, please help, this is my code

const menu = {
  _courses: {
      appetizers: [],
      mains: [],
      desserts: [],
  },
//getters  
  get appetizers() {
    return this._courses.appetizers;
  },
  get mains() {
    return this._courses.mains;
  },
  get desserts() {
    return this._courses.desserts;
  },
//setters 
  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 appetizers = this.getRandomDishFromCourse('appetizer');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, and ${mains.name}, and ${desserts.name} and the price is ${totalPrice}`;
  }
}; 

menu.addDishToCourse('appetizers', 'papas', 5.4);
menu.addDishToCourse('appetizers', 'pan', 7.5);
menu.addDishToCourse('appetizers', 'cascaras', 10.1);

menu.addDishToCourse('mains', 'pescado', 20.6);
menu.addDishToCourse('mains', 'pollo', 25.8);
menu.addDishToCourse('mains', 'carne', 29.8);

menu.addDishToCourse('desserts', 'helado', 30.5);
menu.addDishToCourse('desserts', 'gelatina', 35.9);
menu.addDishToCourse('desserts', 'brownie', 40.0);


let meal = menu.generateRandomMeal();

console.log(meal);

link
https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/meal-maker

Hello!

You’re really close to getting this project completed, but there are some issues in your generateRandomMeal() function with the consistency of variable names. For example, you are creating a variable named appetizers but then you try to access it with appetizer (without the s) when calculating the price. That same issue happened with other variables in that function too.

Your original error message stems from calling this.getRandomDishFromCourse('appetizer') because your array in the object is called ‘appetizers’, so that function couldn’t find the correct array and was instead trying to find the length of undefined.

Stop reading here if you want to go back and try to spot the mistakes in your generateRandomMeal() function. Otherwise, here is a version that works:

  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}, and ${main.name}, and ${dessert.name} and the price is ${totalPrice}`;
  }

thank you so much! i knew it had to be typos, but man i started changing some and keeping others so in the end i had no idea which was what. Thanks again, it works now!

1 Like