Sorry, indeed I did’t paste you the code.
"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 desserts(appetizers) {
this._courses.appetizers = appetizers
},
set desserts(mains) {
this._courses.mains = mains
},
set desserts(desserts) {
this._coursesdesserts = 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._course[courseName].push(dish)
},
getRandomDishFromCourse(courseName) {
const dishes = this.course[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}, and ${dessert.name}, and the total price is ${totalPrice}
}
};"
WIth regards to the second point you explained, it should be clear. Basically, please correct me if I’m not right, when we push an object in an array, then both pieces of information belonging to the object (in this case, name and price) are assumed to be stored in the same index. If this is true, then I understood!
The first part of my question troubles me a bit more, even if it looks a bit easier. ‘generateRandomMeal’ is a function, and as such inside of it we do not have access to the other object’s properties, and thus I do not get why we can use the canonical ‘obj.key’ to access pieces of data that are stored in the array.
I thank you very much in advance for the help!