I have a question regarding Meal Make project. According to the exercise, I had to do the following:
Create an object menu
Create a private object _courses
under “_courses” I had to create three blank arrays “appetizers”, “mains” and “desserts” with no privacy settings and consequently I had to create get/set for each array.
under “menu” object I had to create a get call courses() that returns the values of the arrays in courses.
Now here is the problem:
After doing the whole instructions, the exercise threw a return errors on array references. I compared codes from an existing solution and I noticed that all items in _course object is private. So, the problems got fixed after I made them private too.
One supposes for the extra practice of actually putting them to use. As the code stands now they can be removed, all but get courses. I wouldn’t remove them, though. Try to get them working if you have the time to spend, else bookmark and come back to the project later.
Mine is logging the “Meal or price was not set correctly!” I even did the Unstuck Video and have exactly the same code written for this project. Can you all check what I might have entered incorrectly?
const menu = {
_meal: “”,
_price: 0,
set meal(mealToCheck) {
if (typeof mealToCheck === “string”) {
return (this._meal = mealToCheck);
}
},
set price(priceToCheck) {
if (typeof priceToCheck === “number”) {
return (this._price = priceToCheck);
}
},
get todaysSpecial() {
if (this.meal === true && this.price === true) {
return Today's Special is ${this.meal} for $${this.price}!;
} else {
return “Meal or price was not set correctly!”;
}
},
};
If you’re only trying to check whether this.meal and this.prices are truthy,
you might do something like if (this.meal && this.price) {
You wouldn’t use this.meal === true for that because that would check whether the value is exactly true (meaning same value and same type).
Also, you don’t have getters for meal and price in your code (in the menu object),
so you’d use this._meal instead of this.meal in the todaysSpecial method/getter.
And similarly for this.price
I like that you’re stressing this. Very good point for learners to mark, N. B.
One would discourage the use of backing variables in any setting other the actual getter and setter. Where I run into issues with your explanation is that it ignores the fact that setters don’t work without getters. We need a get method if we have a set method.
The science behind it is a little over my head but it has to do with tying up the binding while the value is being changed so it cannot be polled asynchronously while that is going on. Wrap your head around that for a minute. It still mystifies me, but I accept it, since it works as promised.