Meal Maker project question

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.

Exercise instructions:

_courses {
appetizers: [],
mains: [],
desserts:[],
}

Throws return errors and calling the function addDishToCourse threw an error “addDishToCourse is not a function”

Solution found in various places

_courses {
_appetizers: [],
_mains: [],
_desserts:[],
}

Everything is fine

Can someone tell me why I had to make the items private while the instruction didn’t say so?

Thank you

In general, properties with getters and setters are private so that the variable is available for other use.

foobar = {
  _foo: [],
  _bar: [],
  get foo () {
    return this._foo;
  },
  get bar () {
    return this._bar;
  },
  set foo (newFoo) {
    this._foo.push(newFoo);
  },
  set bar (newBar) {
    this._bar.push(newBar);
  }
}

console.log(foobar.foo, foobar.bar);    // [] []
foobar.foo = 'foo';
foobar.bar = 'bar';
console.log(foobar.foo, foobar.bar);    // ['foo'] ['bar']

Notice that all our calls use the public variable?

Mine works just fine without making them private

Blockquote
const menu={
_courses:{
appetizers:,
mains:,
desserts:,
},
};

and that put a new question in my head:

Why the exercise tells you to create Getters and Setters, but I don’t see we use them!, I see we accessed the _courses={} directly.

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.

1 Like

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!”;
}
},
};

menu._meal = “Pizza”;
menu._price = 24;

console.log(menu.todaysSpecial);

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

2 Likes

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.

2 Likes

Thank you! It worked! I removed the === and also updated the this._meal to this.meal in the todaysSpecial method. Same with for this.price