My meal maker keeps returning ‘undefined’. So I started to look through the individual elements of my object to see where the problem may lie. First of all I checked console.log(menu._courses);
to see if my functions for adding dishes worked. All was fine
And then I tried console.log(menu.getRandomDishFromCourse(menu._courses.appetizers))
to see if .getRandomDishFromCourse
worked, and it didn’t it turned out.
Originally I had written the method like this
getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName]; return dishes[Math.floor(Math.random() * dishes.length)];
I checked the walkthrough video and figured that perhaps I should have broken it down a bit by using variables so now the method looks like this:
getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName]; const randomIndex = Math.floor(Math.random() * dishes.length) return dishes[randomIndex]; },
But… I still have a problem. When I try console.log(menu.getRandomDishFromCourse(menu._courses.appetizers));
I get this error
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38
const randomIndex = Math.floor(Math.random() * dishes.length)
^
TypeError: Cannot read property ‘length’ of undefined
My .length isn’t working and I can’t figure out why… (as the actor said to the bishop).
It’s probably something simple but I’m struggling see the problem
Here’s my full code:
https://gist.github.com/6d0946f2d3494b60659ab1479b6c76bc
I noticed that in your getter methods there is not a return statement:
get appetizers() {
this._courses.appetizers;
},
Add a return statement to the getters methods and see if that helps.
2 Likes
Here is your getRandomDishFromCourse
method. What does it take as a parameter?
getRandomDishFromCourse(courseName) { //courseName should be the name of a single course, no?
const dishes = this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length)
return dishes[randomIndex];
},
Here is your attempted test of the method:
console.log(menu.getRandomDishFromCourse(menu._courses.appetizers));
What are you passing as the argument? You could print it out to see:
console.log(menu._courses.appetizers);
2 Likes
I’ve just tried that and it seems to work fine
Thanks, that’s a problem I didn’t know I had yet but so far it hasn’t fixed my .length problem
Rather strange, it seems my test was in some way faulty because I discovered that my initial problem was that I hadn’t put a return statement into my generateRandomMeal method which was quite a substantial omission. But in my efforts to problem solve one method at a time I managed to confuse myself even more.
I would still like to find out why my .length wasn’t working when I tried to test the method but it must have worked when I generated my random meal
When you tried to test your function, you didn’t pass a single course name as the argument. You passed the entire appetizers array, menu._courses.appetizers
. When your function tried to access this._courses[courseName]
it assigned dishes
to undefined
. Hence your error thrown in the next line when you attempted to get the length of dishes
.
2 Likes
If you are still having a problem with the length property returning undefined then it would be helpful if you could post the updated code after adding return statements and making adjustments to it.
Ah yes, makes sense now. Thanks!
1 Like