Meal maker

Hello I am having problems reading mymsolution to the meal maker project

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,
   };
   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 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 ${appetizers.name}, ${mains.name}, and ${desserts.name} and the total price is ${totalPrice}`;
  }
};

menu.addDishToCourse('appetizers', 'salad', 5);
menu.addDishToCourse('appetizers', 'wings', 7);
menu.addDishToCourse('appetizers', 'fries', 8);

menu.addDishToCourse('mains', 'steak', 11);
menu.addDishToCourse('mains', 'salmon', 12);
menu.addDishToCourse('mains', 'tofu', 10);

menu.addDishToCourse('desserts', 'ice cream', 3);
menu.addDishToCourse('desserts', 'cheesecake', 4);
menu.addDishToCourse('desserts', 'tiramisu', 6);


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

when I want to type that into the console it returns an error at line 41 const randomIndex = Math.floor(Math.random() * dishes.length);
^

TypeError: Cannot read property ‘length’ of undefined
at Object.getRandomDishFromCourse I can’t make it work.

dishes is undefined, where is dishes defined? here:

const dishes = this._courses[courseName];

so something might go wrong here. We could log the courseName and courses key to see what might be the problem:

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

teaching yourself to debug like this, is maybe even more important then getting to solution

where there no other topics covering this? I feel like I sometimes answer this question multiple times per week.

2 Likes

there were some others replies to this specific project but none of them really offer solution.

Anyway thanks. I will have a look at the video help once again

You have an opportunity here to learn an extremely valuable skill. Far more valuable than how to interact with an object in Javascript. You can learn to use the tools available to you to debug your own code. Comparing your code to someone else’s is not a valuable skill. Observing what your code actually does, identifying where things differ from what you expect/want, and making changes to correct the behavior are far more useful than comparing words on a screen line by line to someone else’s.

The error message tells you where to start looking, and @stetim94 has given you a suggestion on how to use console.log() to observe what your code is doing. I’d go with that over comparing code to a video.

2 Likes

Nothing covered that attempting to use a key which doesn’t exist on an object will result in undefined?

which in turn leads to error further down the line (given length is a array specific property)

and even with the console.log I provided, you don’t see it?

you need to learn to debug this :slight_smile: don’t rely on the video. I second @midlindner

3 Likes

thanks for the feedback.

1 Like