Meal Maker Undefined value


#1

Hello everyone,

I’ve a bit of trouble with this project: Meal Maker

I have got following output:
Your meal is undefined, undefined, undefined. The price is $NaN.

Would someone help me with this issue please?

Here my code:

const menu = { 
 _courses: {
   _appetizers: [], 
   _mains: [], 
   _desserts: [],
   
   set appetizers(appetizerIn) {this._appetizers.push(appetizerIn);},
   get appetizers() {return this._appetizers;},
   set mains(mainsIn) {this._mains.push(mainsIn);},    
   get mains() {return this._mains;},
   set desserts(dessertsIn) {this._desserts.push(dessertsIn);}, 
   get desserts() {return this._desserts;},
 }, 
 
 get courses() {
   return {
       appetizers: this._courses.appetizers,  
       mains: this._courses.mains, 
       desserts: this._courses.desserts, 
    };
 },
 
 addDishToCourse (courseName, dishName, dishPrice) {
   	const dish = {
     name: dishName,
     price: dishPrice,      
    // this._courses[courseName].push(dish); // also try using your setter method!
     set name(dishName) {
       if(typeof dishName === 'string'){
           this.name[courseName] = dishName;
         }
         else{
           console.log(`${dishName} is not a valid choice.`);
         }
     },
  		get name() {
       		return dishName;
           console.log(`Great. ${dishName} has been added.`);
    },

     set price(dishPrice){
       if(typeof dishPrice === 'number'){
           this.price[courseName] = dishPrice;
       	}
       	else{
           console.log(`${dishPrice} is not a dollar amount.`);
       	}
      },
  		get price() {
       		return dishPrice;
           console.log(`Great. ${dishPrice} has been added ...`);
     },
    } 
 }, 
 
 getRandomDishFromCourse: function (courseName) {
       const dishes = this._courses[courseName];
       const randomIndex = Math.floor(Math.random() * dishes.length);
       // return a dish from `dishes` by using `randomIndex`
     	return dishes[randomIndex];
   console.log(this._courses[courseName]);
 },
 
 generateRandomMeal: function() {
   const appetizers = this.getRandomDishFromCourse('appetizers');
   const mains = this.getRandomDishFromCourse('mains');
   const desserts = this.getRandomDishFromCourse('desserts');
   const totalPrice = appetizer.price + mains.price + desserts.price;

   return `Your meal is ${appetizer.name}, ${mains.name}, ${desserts.name}. The price is $${totalPrice}.`;
  }

};

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers','Pot Stickers', 6.25);
menu.addDishToCourse('appetizers','Wanton Tacos', 7.25);

menu.addDishToCourse('mains', 'Spaghetti with Sausage and Tomato Sauce', 12.00);
menu.addDishToCourse('mains','Baby Back Ribs', 21.25);
menu.addDishToCourse('mains','Garlic Crusted Chicken', 12.50);

menu.addDishToCourse('desserts', 'An ice cream cake', 4.00);
menu.addDishToCourse('desserts','Blondie', 8.30);
menu.addDishToCourse('desserts','Brownie Sundae', 9.15);

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

#2

Should that really be a template literal? Or should it be,

return dishes[randomIndex];

?

P. S. I gave your topic a vote as a way to say thank you for posting the exercise link.


#3

Thank you, Roy!
You right!
I have corrected that one. But now I have another issue. Here the Error:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68
    const totalPrice = appetizers.price + mains.price + desserts.price;
                       ^

ReferenceError: appetizers is not defined
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68:24)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:87:17)
    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)

#4

What context is line 68 in? The generateRandomMeal method?


#5

Yes, it is generateRandomMeal method.
Line 68 is const totalPrice = appetizers.price + mains.price + desserts.price;


#6

What did you name the variable on line 65?


#7

You right! Thank you, Roy! Corrected. It was ‘appetizer’.
But Error message still appears:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68
    const totalPrice = appetizers.price + mains.price + desserts.price;
                                 ^

TypeError: Cannot read property 'price' of undefined
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:68:34)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:87:17)
    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)

#8

For that function I went with the singular on the variable names…

appetizer
main
dessert

so the above line is,

const totalPrice = appetizer.price + main.price + dessert.price;

#9

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