Meal Maker Poject

hi!

I’m in Meal Maker Project in JavaScript and I have a problem that I cannot solve.

here is my code:

const menue = {
 _courses: {
   appetizers: [],
   mains: [],
   desserts: []
 },
  get appetizers(){
    return this._courses.appetizers;
  },
  set appetizers(appetizerIn){
    this._courses.appetizers = appetizerIn;
  },
  get mains(){
     return this._courses.mains;
  },
  set mains(mainIn){
    this._courses.mains = mainIn;
  },
  get desserts(){
     return this._courses.desserts;
  },
  set desserts(dessertIn){
    this._courses.desserts = dessertIn;
  },
  //for 5 and 6
  get courses(){
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    };
  },
  
  //7 and 8
   addDishToCourse (courseName ,dishName ,dishPrice) {
     const dish = {
      name: dishName,
      price: dishPrice
    }
     return this._courses[courseName].push(dish);
  },
  
  //9
  getRandomDishFromCourse(courseName) {
    const dishes = this._courses[courseName];
   const rondomIndex = Math.floor(Math.random() * dishes.length);
    return dishes[randomIndex];
  },
  
  //11
  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}, and ${dessert.name}. The price is $${totalPrice}.`;
  },
  
};

menue.addDishToCourse('appetizers' , 'salad' , 4.5);
menue.addDishToCourse('appetizers' , 'wings' , 5.5);
menue.addDishToCourse('appetizers' , 'yogurt' , 4);

menue.addDishToCourse('mains' , 'pizza' , 14.5);
menue.addDishToCourse('mains' , 'pide' , 7.5);
menue.addDishToCourse('mains' , 'steak' , 24.5);

menue.addDishToCourse('desserts' , 'shole zard' , 6.5);
menue.addDishToCourse('desserts' , 'cake' , 6);
menue.addDishToCourse('desserts' , 'baklava' , 4);


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

and here is the error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:46
const rondomIndex = Math.floor(Math.random() * dishes.length);
^

TypeError: Cannot read property ‘length’ of undefined

could anyone tell me what is my mistake? or what should I do to figure it out.
Thank you

Hello, @samansbz.

Welcome to the forum.

For future reference, please follow the guidelines in this post for formatting your code: How do I format code in my posts?

Look closely at your generateRandomMeal() function. What are the arguments passed to your getRandomDishFromCourse() method. Do they match the names of the courses?

dear @midlindner,

Thank you so much, yes my arguments don’t match and I fixed it

generateRandomMeal(){
    const appetizer = this.getRandomDishFromCourse('appetizers');
    const main = this.getRandomDishFromCourse('mains');
    const dessert = this.getRandomDishFromCourse('desserts');

also instead of random I typed rondom!
I don’t know why I did that mistakes but I think 9 hours studying and looking at monitor was not completely innocent! :slight_smile:

I appreciate your help.

1 Like

You’re welcome! Glad you got it working!

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.