Meal Maker

Hi there,

I’m desperately stuck trying to get this ‘Meal Maker’ project done. I keep getting an error of ‘Cannot read property length of undefined’ at this point of the code:

const randomIndex = Math.floor(Math.random() * dishes.length);



It’s nested under the getRandomDishesFromCourse method. I’ve checked it against the solutions video, and it looks exactly the same to me. Hoping a pair of fresh eyes can clarify the problem! Thanks.

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: courseName,
        price: dishPrice
    };
   this._courses[courseName].push(dish);
  },
  
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
   const randomIndex = Math.floor(Math.random() * dishes.length);
   return dishes[randomIndex];
 
  },
generateRandomMeal (){
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let dessert = this.getRandomDishFromCourse('dessert');
    let totalPrice= appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is ${totalPrice}`;

}
  
}

menu.addDishToCourse('appetizers','salad',5.00)
menu.addDishToCourse('appetizers','wings',4.00)
menu.addDishToCourse('appetizers','soup',4.50)



menu.addDishToCourse('mains','steak',22.00)
menu.addDishToCourse('mains','salmon',17.00)
menu.addDishToCourse('mains','chicken',15.00)

menu.addDishToCourse('desserts','ice cream',3.75)
menu.addDishToCourse('desserts','brownie',4.00)
menu.addDishToCourse('desserts','pie',4.00)

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

Hello, @eranders, and welcome to the forums.

Rather than proof reading, or comparing your code to someone else’s code, try observing what your code actually does. You can do that by using console.log(). You might give this a try:

Thanks for your reply.

I guess I don’t really understand what’s going on with this code snippet. I thought “courseName” would be a variable I could pass through the function of getRandomDishFromCourse. So if I console.log(courseName), I get nothing. But isn’t that expected, since courseName is a variable?

Sorry for the late reply. I didn’t notice that you had replied to me. When I run your code with the console.log statements I added as shown above the output is as follows:

appetizers
[ { name: ‘appetizers’, price: 5 },
{ name: ‘appetizers’, price: 4 },
{ name: ‘appetizers’, price: 4.5 } ]
mains
[ { name: ‘mains’, price: 22 },
{ name: ‘mains’, price: 17 },
{ name: ‘mains’, price: 15 } ]
dessert
undefined

So, when courseName is assigned to ‘appetizers’, dishes is assigned to an array of objects. Same when courseName is assigned to ‘mains’, but when courseName is assigned to ‘dessert’ we see the source of your error. dishes is being assigned to undefined instead of an array of objects. undefined has no length property, so you see the error message. Where did ‘dessert’ come from? That would be here:

You passed it as the argument to your getRandomDishFromCourse() function. Is ‘dessert’ what you meant to pass?