const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers(){
this._courses.appetizers;
},
get mains(){
this._courses.mains;
},
get desserts(){
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 _course(){
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 cost = appetizer.price + main.price + dessert.price;
return `Your meal is ${appetizer.name},
${main.name}, and ${dessert.name} and total price is ${cost}`;
}
};
menu.addDishToCourse('appetizers', 'wings', 4.00);
menu.addDishToCourse('appetizers', 'tacos', 4.00);
menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('mains', 'steak', 10.00);
menu.addDishToCourse('mains', 'fish', 10.00);
menu.addDishToCourse('mains', 'pasta', 10.00);
menu.addDishToCourse('dessert', 'cake', 6.00);
menu.addDishToCourse('dessert', 'shake', 6.00);
menu.addDishToCourse('dessert', 'pie', 6.00);
let meal = menu.generateRandomMeal();
console.log(meal);
then the first question is: What results in undefined? We get undefined when we try to look-up/access a property/key on an object which does not exists, so then the first step to debug would be to:
- log available keys/properties
- log keys/properties you attempt to use
so we insert a .log()
:
addDishToCourse(courseName, dishName, dishPrice){
const dish = {
name: dishName,
price: dishPrice,
};
console.log(Object.keys(this._courses), courseName);
return this._courses[courseName].push(dish);
},
I think that is quite clear, no?
Try to do what stetim94
says, but I’d add that the problem is that you pass a non-existent field name to addDishToCourse
.