Welcome to the Get Help category!
This is where you can ask questions about your code. Some important things to remember when posting in this category
- Learn how to ask a good question and get a good answer!
- Remember to include a link to the exercise you need help with!
- If someone answers your question, please mark their response as a solution
- Once you understand a new concept, come back and try to help someone else!
Hi, my name is Joe I am working on the Javascript beginners course and coming towards the end of the objects module. I am doing the meal maker project and I can’t find where I am going wrong. Any help would be greatly appreciated.
Thank you.
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: []
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(appitizerIn) {
this._courses.appetizers = appetizersIn;
},
get mains() {
return this._courses.mains;
},
set mains(mainIn) {
this._courses.mains = mainIn;
},
get desserts() {
return this._courses.desserts;
},
set desserts(dessertsIn){
this._courses.desserts = dessertsIn;
},
get courses() {
return{
appetizers: this.appetixers,
mains: this.mains,
desserts: this.desserts
}
},
addDishToCourse(courseName, dishName, dishPrice){
const dish = {
name: dishName,
price: dishPrice
};
this._courses[courseName].push(dishName);
},
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 ${appetizer.name}, ${main.name}, ${dessert.name} and your total price is ${totalPrice}.`
}
};
menu.addDishToCourse('appetizers', 'prawn cocktail', 5.26);
menu.addDishToCourse('appetizers', 'wings', 6.25);
menu.addDishToCourse('appetizers', 'gralic bread', 4.50);
menu.addDishToCourse('main', 'steak', 11.60);
menu.addDishToCourse('main', 'burger', 9.60);
menu.addDishToCourse('main', 'pizza', 10.60);
menu.addDishToCourse('dessert', 'ice cream', 3.50);
menu.addDishToCourse('dessert', 'cake', 4.50);
menu.addDishToCourse('dessert', 'brownie', 6.50);
const meal = menu.generateRandomMeal();
console.log(meal);
I am getting the following error.
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
this._courses[courseName].push(dishName);
^
TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:28)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:57: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)