Meal Maker Project Code

Hello,

I am working on the meal maker project for the JavaScript module and for some reason my code just does not want to work when it comes to the addDishToCourse part.

Can anyone please take a look at my code and help me figure out what I might be missing.

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.length);
return dishes[randomIndex];
},
generateRandomMeal() {
const appetizer = this.getRandomDishFromCourse(‘appetizers’);
const main = this.getRandomDishFromCourse(‘mains’);
const dessert = this.getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + main.price + dessert.price;
return Your meal is ${appeitzer.name}, ${main.name}, ${dessert.name}, and the total price is ${totalPrice};
},

};

menu.addDishToCourse(‘appetizers’ + ‘salad’ + 5.00);
menu.addDishToCourse(‘appetizers’ + ‘wings’ + 11.00);
menu.addDishToCourse(‘appetizers’ + ‘bruschetta’ + 15.00);

menu.addDishToCourse(‘mains’ + ‘filet mignon’ + 35.00);
menu.addDishToCourse(‘mains’ + ‘salmon’ + 25.00);
menu.addDishToCourse(‘mains’ + ‘burger’ + 15.00);

menu.addDishToCourse(‘desserts’ + ‘cheesecake’ + 12.00);
menu.addDishToCourse(‘desserts’ + ‘creme brulee’ + 12.00);
menu.addDishToCourse(‘desserts’ + ‘coffee’ + 3.50);

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

Hello @thrice711, welcome to the forums! How do you mean, doesn’t work? If it’s throwing an error, what is the error? Otherwise, what’s happening that shouldn’t (or what’s not happening)?

Hi, thank you for replying!

Here is the error it gives me:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:34
return this._courses[courseName].push(dish);
^

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:34:35)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:51:6)
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)

Notice how when you’re passing in arguments to your addDishToCourse function, you’re actually concatenating the arguments:

menu.addDishToCourse(‘appetizers’ + ‘salad’ + 5.00);
                                  ^         ^
These plus signs mean concatenation

That means, for the above example, you’re actually passing in the string "appetizerssalad5" as the first argument.

Remember that when you’re calling a function, the arguments are separated by commas:

someFunc(arg1, arg2, arg3)
1 Like

That worked perfectly. Thank you!

1 Like