Meal Maker exercise problem

Hello Everybody,

Today I’ve approached and started the Meal Maker exercise on the object category but unfortunately i’m pretty stuck with an error that i keep getting from the console and i was just wondering if anybody could help me with it. I have read the forum and found another similar question and also compared the solution given to my code and it seems correct but i keep getting the error. :grimacing:

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

Here’s the code i wrote :

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 {
    appetizer: 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: function(){
const appetizer = this.getRandomDishFromCourse("appetizer");

const main = this.getRandomDishFromCourse("mains");

const dessert = this.getRandomDishFromCourse("desserts");

const totalPrice = appetizer.price + mains.price + dessert.price;
return `Oggi il ristorante da Gianmarco propone come antipasto  ${appetizer.name}, seguito da ${main.name} e per terminare come dessert abbiamo ${dessert.name}. Il tutto ad € ${totalPrice} a persona.`;
}
};

menu.addDishToCourse('appetizers', 'Prosciutto di Parma DoP stagionato 36 mesi, Tagliato a mano', 12);
menu.addDishToCourse('appetizers', 'Cuoppo Napoletano Fritto Misto 6 Pz', 8);
menu.addDishToCourse('appetizers', 'Insalata Caprese con Mozzarella di Bufala di Battipaglia e olive di Gaeta', 12);
menu.addDishToCourse('appetizers', 'Antipasto misto della casa (montagna)', 10);


menu.addDishToCourse('mains', 'Paccheri freschi con polpo nostrano, olive di Gaeta e pomodorini del piennolo', 15);
menu.addDishToCourse('mains', 'Gnocchi di patate viola con vongole veraci raccolte a mano', 13);
menu.addDishToCourse('mains', 'Pappa al Pomodoro', 8);


menu.addDishToCourse('desserts', 'Tiramisù', 5);
menu.addDishToCourse('desserts', 'Panna Cotta', 6);
menu.addDishToCourse('desserts', 'Torta della Casa', 4);

const meal = menu.generateRandomMeal();

console.log(meal);

And here’s the error i keep getting :

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

TypeError: Cannot read property 'length' of undefined
    at Object.getRandomDishFromCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:41:53)
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:45:24)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:71:19)
    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)

Can anybody please help me out to understand what i did wrong? :pray: :pray: :pray: :pray: :sob:

Thank you so so so much in advance and best regards from Italy!

Your mistake is the all time favorite here :wink:
Add this console to your getRandomDishFromCourse method.

console.log(courseName, Object.keys(this._courses));

courseName is the argument you receive from the function call. Object.keys(this._courses) gives you all the keys from the _courses object, where you want to get the dishes from with this._courses[courseName].
That will help you to find it.

2 Likes

Thank you Mirjia for your help.

I was able to find the error and i was not seeing it because was a typo error.

Again, thank you.

1 Like

Yes, mixing up plural and singular twice. That is extremely difficult to find manually. That’s when debugging with consoles comes in handy. When you add them to all methods in question they help you spotting where a variable gets lost or deviates from the target. While simply spell checking is tedious and error prone.

1 Like

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