Meal Maker

I need some help deciphering this code. I have been spiraling out figuring out what the cause of this error.

Screenshot 2021-08-06 175553

and this is the code that i made. the error is the same when i watched the tutorial for this exercise.

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.lenght); return dishes[randomIndex]; }, generateRandomMeal() { const appetizers = this.getRandomDishesFromCourse('appetizers'); const mains = this.getRandomDishesFromCourse('mains'); const desserts = this.getRandomDishesFromCourse('desserts'); const totalPrice = appetizer.prize - main.prize - - dessert.prize; return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}`; } }; menu.addDishToCourse('appetizer', 'salad', 4.00); menu.addDishToCourse('appetizer', 'wings', 8.00); menu.addDishToCourse('appetizer', 'fries', 5.00); menu.addDishToCourse('mains', 'steak', 23.00); menu.addDishToCourse('mains', 'salmon', 34.00); menu.addDishToCourse('mains', 'tofu', 20.00); menu.addDishToCourse('dessert', 'cheesecake', 14.00); menu.addDishToCourse('dessert', 'sorbet', 9.00); menu.addDishToCourse('dessert', 'lava cake', 18.00); const meal = menu.generateRandomMeal(); console.log(meal);

it would help if someone can point out to me what’s wrong with my code.

check slowly line by line for typos and that the formulas are correct and you will find the errors.

i did and i still dont know what’s wrong

this is not the answer but the errors I noticed are within these code blocks below. Check them carefully. I have been warned not to give only answers as I might get flagged and the messages will be deleted as stated by codecademy.

getRandomDishFromCourse(courseName) {
const dishes = this._courses[courseName];
→ const randomIndex = Math.floor(Math.random() * dishes.lenght);
return dishes[randomIndex];
},
generateRandomMeal() {
const appetizers = this.getRandomDishesFromCourse(‘appetizers’);
const mains = this.getRandomDishesFromCourse(‘mains’);
const desserts = this.getRandomDishesFromCourse(‘desserts’);
→ const totalPrice = appetizer.prize - main.prize - - dessert.prize;
return Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice};
}
};

1 Like

okay, im gonna try, but i really dont know what the syntax their talking about. i followed the video back and forth and they still gave me the same error message

You have a lot of spelling errors …

dessert should be desserts in some places
appetizer should be appetizers in some places

getRandomDishesFromCourse does not match getRandomDishFromCourse

prize should be price in the last function
there’s a minus where you should have a plus

length is spelled incorrectly

and maybe others

You should check these line by line.

also, in the addDishToCourse function:
just in case you can’t get this._courses[courseName] to work …
here’s another way to attempt it:

a bunch of if statements (if-else) checking for each possible value of courseName

like this one:

    if (courseName == 'appetizers') {
      this._courses.appetizers.push(dish); 
    }

Hi,

thank you for your response. i examined my mistakes and i did have a lot of spelling errors and i did my best to change them. what i dont understand is that why some parameters have to be plural and singular form? and i also went with your advise to the code block

if (courseName == ‘appetizers’) {
this._course.appetizers.push(dish)

btw, this is my the latest render:

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, }; if (courseName === 'appetizers') { this._courses.appetizers.push(dish); } }, getRandomDishFromCourse(courseName) { const dishes = this._courses[courseName]; const randomIndex = Math.floor(Math.random() * dishes.lenght); return dishes[randomIndex]; }, generateRandomMeal() { const appetizer = this.getRandomDishFromCourse('appetizers'); const main = this.getRandomDishFromCourse('mains'); const dessert = this.getRandomDishFromCourse('desserts'); const totalPrice = appetizers.price - mains.price - desserts.price; return `Your meal is ${appetizers.name}, ${mains.name}, and ${desserts.name}, and the total price is ${totalPrice}`; } }; menu.addDishToCourse('appetizers', 'salad', 4.00); menu.addDishToCourse('appetizers', 'wings', 8.00); menu.addDishToCourse('appetizers', 'fries', 5.00); menu.addDishToCourse('mains', 'steak', 23.00); menu.addDishToCourse('mains', 'salmon', 34.00); menu.addDishToCourse('mains', 'tofu', 20.00); menu.addDishToCourse('dessert', 'cheesecake', 14.00); menu.addDishToCourse('dessert', 'sorbet', 9.00); menu.addDishToCourse('dessert', 'lava cake', 18.00); let meal = menu.generateRandomMeal(); console.log(meal);

it does not matter if you choose the parameter to be plural or singular,
however, whichever one you choose, you should keep using it
meaning:
if you name a variable appetizers
and later you have appetizer

the computer won’t recognize it as the same variable

so its important to spell the variable name the same way each time

And there’s still spelling errors.

length is still misspelled in the function getRandomDishFromCourse

(line 50 for example)
it should be appetizer (because that’s the variable name you used earlier in that function)
same for mains and desserts on that line … those should be main and dessert because those are the names used earlier in the function
those minuses should probably be pluses
(similarly for line 51)

lines 63, 64, 65 - check your spelling

also, in the addDishToCourse function:
you have

    if (courseName === 'appetizers') {
      this._courses.appetizers.push(dish);
    }

which is fine,
but that only takes care of appetizers
you have to do the same thing [in that function] for mains and desserts

1 Like

if you get an error like “that property is undefined” in the generateRandomMeal function
you can deal with that by creating dealing with the case of the variable not existing
here’s one strategy:

let appetizer = this.getRandomDishFromCourse('appetizers');
if (!appetizer) {
   appetizer = { name: 'none', price: 0 };
}

This works because if a variable does not exist, (or is empty) it is falsy (meaning it evaluates to false when you do if( that_variable )
so appetizer would work like false if this.getRandomDishFromCourse('appetizers'); fails and the variable or object isn’t created (or is empty)
in that situation, !appetizer would be true

I only did that for appetizer
you would have to do similar stuff for the other two: main and dessert

Notice that I used let instead of const here, because I want to be able to change the value of the variable using the if-statement.

1 Like