Meal Maker


#1

Hello, i have a problem with the Meal Maker (https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?course_redirect=introduction-to-javascript)
I always get an this:

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

TypeError: Cannot read property 'length' of undefined

So in order to find out why the object is undefined I added a console.log(dishes) at line 45. It logs this:
[ { name: ‘apt1’, price: 2 },
{ name: ‘apt2’, price: 4 },
{ name: ‘apt3’, price: 5 } ]
undefined

I read through all the other topics for meal maker, but they were all different problems. hope somebody can help

const menu = {
 _courses: {
   _appetizers:[],
   _mains: [],
   _desserts:[],
   
  	get appetizers() {
   return this._appetizers;
 	},
 	get mains() {
 		return this._mains;
   	},
   get desserts() {
     return this._desserts;
   },
   set appetizers(appetizerIn){
   	this._appetizers.push(appetizerIn);
 	},
 	set mains(mainIn){
   	this._mains.push(mainIn);
 	},
   set desserts(dessertIn){
     this._desserts.push(dessertIn);
   }
 },
 
 get courses() {
   return {
     apptizer: this._courses.appetizers,
     mains: this._courses.mains,
     dessert: this._courses.desserts
   };
 },
 
 addDishToCourse: function (courseName, dishName, dishPrice){
   const dish = {
     name:dishName,
     price:dishPrice
   };
   this._courses[courseName].push(dish);
 },
 
 getRandomDishFromCourse: function (courseName){
   const dishes = this._courses[courseName];
   console.log(dishes);
   const randomIndex = Math.floor(Math.random() * dishes.length);
   return dishes[randomIndex];
 },
 
 generateRandomMeal (){
 	const appetizer = this.getRandomDishFromCourse('appetizers');
   const main = this.getRandomDishFromCourse('main');
   const dessert = this.getRandomDishFromCourse('dessert');
   const price = appetizer.price+main.price+dessert.price;
   return 'You have ordered '+ appetizer.name+ ' , '+ main.name+' , '+ dessert.name+' and it costs '+ price+' .';
   }
};
menu.addDishToCourse('appetizers', 'apt1', 2);
menu.addDishToCourse('appetizers', 'apt2', 4);
menu.addDishToCourse('appetizers', 'apt3', 5);

menu.addDishToCourse('mains', 'man1', 6);
menu.addDishToCourse('mains', 'man2', 8);
menu.addDishToCourse('mains', 'man3', 10);

menu.addDishToCourse('desserts', 'des1', 3);

menu.addDishToCourse('desserts', 'des2', 5);

menu.addDishToCourse('desserts', 'des3', 7);
const meal = menu.generateRandomMeal();
console.log(meal);

#2

Having figured out that this._courses[courseName] evaluates to undefined, you might next check what this._courses refers to and what courseName is, and whether that’s a key in this._courses, and keep following whatever isn’t what it should be


#3

Sorry, but I just don’t see it. I don’t even understand how the output is possible like that - the array and then outside of the array an “undefined”.
this._courses refers to the courses Object and courseName refers to the selected property (main, dessert or appetizer). I already read all the hints and I can’t find any difference.
I need a little bit more input, I have looked at this problem now for hours and I just don’t see it


#4

Accessing an attribute that doesn’t exist in an object will result in undefined:

console.log({}.nosuchattribute);  // undefined

Where {} is an object (empty)
The very same thing could also be written as:

console.log({}['nosuchattribute']);  // undefined

The above should already be familiar for you. Objects are basic building blocks in js.

Point is, you meant to look up some value, and didn’t get it. So what you should be doing is to look at where it should have come from and see if it is there.

I want some value, I’m not getting it. Where should it have come from? Go look there and see if everything’s alright -> repeat until problem found

Sit down -> whoops no chair. -> ask sister why she didn’t bring chair -> there was no chair in the kitchen -> why do you not have a chair -> check what happened with ikea delivery …


#5

Thanks for the hint, I got it know, I missed the S in mainS in generateRandomMeal. I guess it was just to late yesterday and I was frustrated, so I couldn’t see it.


#6

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