I’m struggling with this one - I get the suggested solution but I am trying to create the function .addDishToCourse() using setters created earlier…
My code looks like this:`
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
//Getter methods
get appetizers() {
return this._courses.appetizers;
},
get mains() {
return this._courses.mains;
},
get desserts() {
return this._courses.mains;
},
//setters
set appetizers(appetizerIn) {
this._courses.appetizers.push(appetizerIn);
},
set mains(mainIn) {
this._courses.mains.push(mainIn);
},
set desserts(dessertIn) {
this._courses.desserts.push(dessertIn);
},
get courses() {
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts,
}
},
addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: this.dishName,
price: this.dishPrice,
};
//return this._courses[courseName].push(dish);
menu.[courseName] = {dishName, dishPrice};
},
//getRandomDishFromCourse(courseName) {
};
menu.addDishToCourse('appetizers', 'salad', 30.00);
How do I call the setter taking the argument from the addDishToCourse() function? I want to call the setter for appetizers if courseName is appetizer and so forth. I could write an if-else statement but that seems clumsy. I thought that I’d be able to do menu.[courseName] = {dishName, dishPrice} but this throws an error. You’ll see I changed the suggested format of the getter to push the value into the array.