Error with Meal Maker

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(courseName){
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 total is $${totalPrice} and your meals are ${appetizer.name}, ${main.name}, ${dessert.name},
}
};

I have an error with the price : /home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48
const totalPrice = appetizer.price + main.price + dessert.price;
^

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:48:46)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:57:17)

what should I do ?

Recall that those three properties are array objects. Direct assignment will destroy the data structures.

This method should not return. If the push method is deployed i the setters, we can use standard property value assignment:

this._courses[courseName] = dish;

For the record, this particular module has been discussed a lot over the past couple of years. If you have time, browse the topics to uncover the many points raised in those discussions.

ok! Thank you very much!! :slightly_smiling_face:

But how should I do then? Sorry haha I am a beginner :grimacing:

I’ve changed 2 things at line 17, 20, 23 and line 37. Still get the same error… :thinking:

  set appetizers(appetizers){
    this._courses.appetizers = "appetizer"; 
  }, 

Something one would never recommend is using a variable (parameter) with the same name as the variable we are setting.

set appetizers (appetizer) {

The purpose of this setter is to add items to the array. Your code above is replacing the array with a single string. We never write quotes around a variable name.

    this._courses.appetizers.push(appetizer)

Aside

If you have skipped any modules then best to set this project to the side and go back through the lesson modules. If you haven’t skipped anything, then still best to go back and do a full review of all the concepts. Variables, data types, objects, arrays, functions and methods.

Ok thank you for your quick replies!!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.