Meal maker JS

Hello im having trouble with this exercise, if someone can help I would appreciate it, here is the link to my exercise.

my code is as follows
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,

    };

    this._courses[courseName].push(dish);

  },

  getRandomDishFromCourse(courseName){

    const dishes = this._courses[courseName];

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

    return dishes[randomIndex];

  },

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

  }

};

  menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);

  menu.addDishToCourse('appetizers', 'Scampi', 5);

  menu.addDishToCourse('appetizers', 'Picada', 4.25);

  menu.addDishToCourse('mains', 'Rib Eye Steak', 25);

  menu.addDishToCourse('mains', 'Homard', 29);

  menu.addDishToCourse('mains', 'Salmon', 27);

  menu.addDishToCourse('desserts', 'Ice Cream ', 3);

  menu.addDishToCourse('desserts', 'Lemon Pie', 5);

  menu.addDishToCourse('desserts', 'Marquise', 6);

 let meal = menu.generateRandomMeal();

console.log(meal);

the errors says

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:49
const totalPrice = appetizer.price + main.price + dessert.price;
^

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:49:33)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68:18)
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)
at startup (bootstrap_node.js:151:9)

Ive tried several things to change this but still cant make it work, Please help!
Thanks in advance

Hello, @ignacioboudou1215815, and welcome to the forums!

The error message is a great place to start. undefined has no price property. So, what was supposed to be defined that wasn’t? Using console.log you can make observations regarding what your code is doing, and compare that to what you meant for it to do. I would start with something like this:

  generateRandomMeal(){
    const appetizer = this.getRandomDishFromCourse('appetizers');
    console.log(appetizer); //debug print
    const main = this.getRandomDishFromCourse('mains');
    console.log(main); //debug print
    const dessert = this.getRandomDishFromCourse('desserts');
    console.log(dessert); //debug print
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;
  }
2 Likes

Thanks for your answer!
i’ve tried that, the three of them say undefined,and thats what i dont know how to fix, becuase i think it should be conected to the getRandomDishFromCourse(courseName) method that at the same time takes its propertys from the addDishToCourse(courseName, dishName, dishPrice){ method that is suposed to define the price of dishes.
again i apreciate your help, its been quite a struggle, even looking at the video help couldnt find a solution.

1 Like

That is correct. This method is returning undefined each of the three times you call it, so let’s look at it:

  getRandomDishFromCourse(courseName){
    //first let's make sure we sent a proper argument to be assigned to courseName
    console.log(courseName); //debug print
    const dishes = this._courses[courseName];
    // We've just made an assignment to a variable, so let's check that too:
    console.log(dishes); //debug print
    const randomIndex = Math.floor(Math.random() * dishes.lenght);
    //here's another assignment, so let's check it too
    console.log(randomIndex); //debug print
    return dishes[randomIndex];

  },
2 Likes

again thank you so much for your time! i debuged that part and my result was the following:
appetizers
[ { name: ‘Caesar Salad’, price: 4.25 },
{ name: ‘Scampi’, price: 5 },
{ name: ‘Picada’, price: 4.25 } ]
NaN
mains
[ { name: ‘Rib Eye Steak’, price: 25 },
{ name: ‘Homard’, price: 29 },
{ name: ‘Salmon’, price: 27 } ]
NaN
desserts
[ { name: 'Ice Cream ', price: 3 },
{ name: ‘Lemon Pie’, price: 5 },
{ name: ‘Marquise’, price: 6 } ]
NaN

does that mean that the randomIndex doesnt work? cause i tried to fix it but i couldnt, it seems the value courseName works so i think the failiure is that the getRandomDishFromCourse(courseName){, doesn’t properly gives a value to the propertys of _course, is that right?

1 Like

It means that randomIndex is being assigned to NaN instead of what you meant for it to be assigned to. That’s very good to know because now we have the problem narrowed down to a single line of code:

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

Look at each part of this assignment. Does anything seem off? We can print each part if necessary, but you probably won’t need to. It’s totally fine if you do though. Something like:

console.log(Math.random()); //should print a float (0.1835739422112319 for example)
console.log(dishes.lenght); //hmm.. what will this print?
1 Like

Thank you so much for your patience and for walking me through it, the function length wasnt working because of a misstype, that i just noticed :rofl: ,
have a nice day thanks again for the help.

1 Like

You’re welcome. It may have seemed like a long way around to find a simple typo, but the steps I showed you are basically the steps I took to find the typo. Granted, I did it in a few seconds, and didn’t need to print everything, but starting with the error message, and working your way back through your code is the key to debugging. Proofreading is a great and valuable skill for proofreaders, whereas debugging is a great and valuable skill for programmers. :grinning:

Keep up the good work, and happy coding!

1 Like

yes i apreciate the gesture, thanks for the advice found it really usefull

1 Like