Meal Maker Mistake but still works?

This worked but I made a mistake in the courses getter method. I have a typo in the desserts key, assigning this.mains instead of this.desserts. I barely understand how the getter and setter methods actually work let alone why it still works if a key is assigned wrong. I commented that section out to see if it would still work but it threw an error. IT still needS to be there, but how does it function with a bad assignment?

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.mains <–MISTAKE
}
},
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 mains = this.getRandomDishFromCourse(‘mains’);
const desserts = this.getRandomDishFromCourse(‘desserts’);
const totalPrice = appetizer.price + mains.price + desserts.price
return Your meal is a ${appetizer.name}, a ${mains.name}, and a slice of ${desserts.name}: your total bill is $${totalPrice}.
}
};
menu.addDishToCourse(‘appetizers’, ‘Caesar Salad’, 4.25);
menu.addDishToCourse(‘mains’, ‘Steak’, 24.75);
menu.addDishToCourse(‘desserts’, ‘Apple Pie’, 5.00);

let meal = menu.generateRandomMeal();
console.log(meal);

From what I see you do not use the getters and setters at all. That means that you should be able to remove them from your object without getting an error. Maybe you removed a comma too much?
The exercise asks you to write getters and setters as they can be useful, but in my opinion, provides very little information as to why you should define them. You push your courses directly to the _courses object with the function addDishToCourse.
Getters and setters can be useful in complex projects because they provide an easy-to-understand way of which properties you intended to add, remove or alter retrospectively when you wrote your code.

Yep, you’re right.
I commented out all the getters and setters and it still ran.
Seems kinda pointless to have an exercise with no functionality.
I’m going to review the objects lesson and try to gain some better understanding of this.

Thanks for your help!

1 Like

First, please use the triple “```” tilde open and closing tags to create a code section; This will allow proper formatting.

generateRandomMeal () {
    const appetizer = this.getRandomDishFromCourse(‘appetizers’);
    const mains = this.getRandomDishFromCourse(‘mains’);
    const desserts = this.getRandomDishFromCourse(‘desserts’);
    const totalPrice = appetizer.price + mains.price + desserts.price
    return Your meal is a ${appetizer.name}, a ${mains.name}, and a slice of ${desserts.name}: your total bill is $${totalPrice}.
}