Meal Maker

Hello, I’ve been trying to workout an error I get while solving the meal maker exercise, and I keep receiving this error which I can’t figure out how to fix:

 /home/ccuser/script.js:37
    this._courses.courseName.push(dish);
                             ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse (/home/ccuser/script.js:37:30)
    at Object.<anonymous> (/home/ccuser/script.js:53:6)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47

Link to said exercise and a codebyte

const menu = { _courses: { appetizers: [], mains: [], desserts: [], }, get appetizers() { return this._courses.appetizers; }, set appetizers(appetizersIn) { this._courses.appetizers = appetizersIn; }, get mains() { return this._courses.mains; }, set mains(mainsIn) { this._courses.mains = mainsIn; }, get desserts() { return this._courses.desserts; }, set desserts(dessertsIn) { this._courses.desserts = dessertsIn }, get courses() { return { appetizers: this.appetizers, mains: this.mains, desserts: this.desserts, }; }, addDishToCourse (courseName,dishName,dishPrice) { const dish = { name: dishName, price: dishPrice, }; this._courses.courseName.push(dish); }, getRandomDishFromCourse (courseName) { const dishes = this._courses[courseName]; const index = Math.floor(Math.random() * dishes.length) return dishes[index] }, generateRandomMeal(){ const appetizer = menu.getRandomDishFromCourse(`appetizers`); const main = menu.getRandomDishFromCourse(`mains`); const dessert = menu.getRandomDishFromCourse(`desserts`); const price = appetizer.price + main.price + desserts.price; return `Appetizer is ${appetizer}, Main course is ${main}, and dessert is ${dessert}. Total is ${price}`; } }; menu.addDishToCourse(`appetizers`,`Ceasar Salad`,10); menu.addDishToCourse(`appetizers`,`Garlic Bread`, 1); menu.addDishToCourse(`appetizers`,`Pasta Soup`,8); menu.addDishToCourse(`mains`,`Lasagna`,15); menu.addDishToCourse(`mains`,`Pizza`,12); menu.addDishToCourse(`mians`,`Spaghetti Bolognese`); menu.addDishToCourse(`desserts`,`Tartuffo`,6); menu.addDishToCourse(`desserts`,`Cheesecake`,9); menu.addDishToCourse(`desserts`,`Tiramisu`,5); const meal = menu.generateRandomMeal(); console.log(meal);

You have typos.

On line 59 of the Codebyte, you had mians instead of mains.

In the generateRandomMeal function (on line 48)
desserts should be dessert.

In the addDishToCourse function (on line 37)
this._courses.courseName.push(dish);
should be
this._courses[courseName].push(dish);
because the property name you’re using should be a variable, not the string "courseName".

Also, in the generateRandomMeal function, on the last line (line 49),
you have

return `Appetizer is ${appetizer}, Main course is ${main}, and dessert is ${dessert}. Total is ${price}`;

but appetizer, main, and dessert are objects;
the name of each dish is stored as the name property,
So appetizer should be appetizer.name on that line, and so on.

1 Like

Thank you! I did this late at night which explains the typos, and thank you for you advice overall! Sorry for the late reply, you’re awesome