Meal maker

Hello All,

Maybe it is just me but i found the meal maker very difficult to work on. I need a lot of help from the hints and video tutorial but still, at the end i have undefined dishes. Code as below:

Also i found it hard to think it out by myself, why use all the steps including:

_courses,
setter and getter,
this._courses[courseName].push(dish)
generateRandomeMeal
…etc.

Is it just due to my lack of practice and knowledge or it is something easier for one to connect the dots and it is not for me?

This session is quite frustrating and the tutorial doesn’t really help to explain better than the hints…

Thanks…!

The code works for the most part, no typo there. Only thing I’ve found is you seemingly got lost in definition, specifically in not defining name. :slight_smile:

All it needs to work is changing :
return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;

to
return `Your meal is ${appetizer.dish}, ${main.dish}, ${dessert.dish}. The price is $${totalPrice}.`;

(dish instead of name)

Or rename dish into name here instead:

dish: dishName, // -> name: dishName,
price: dishPrice,

About the steps, they’re not needed for the code to work but for the programmer to work with the code.

_courses has that _ in order to tell anyone reading that code that _courses should not be changed, you can change it but the code has a high risk of not working anymore. That’s more of a convention than it is needed for JavaScript.

You could access _courses directly, anyway. According to the lesson texts about setters and getters they, too, help to make the functionality of our code easier to understand for other developers.

I believe, the exercise is trying to make the transition to classes easier for us, classes will follow after setters / getters / _variables, building up on those.

It’s important that our variables have underscores if they are to have a getter/setter else we end up with circular references that will cause a fatal error.

The video example does not use the setters, only the courses getter.

return {
    appetizers: this._courses.appetizers
    ...
}
1 Like

Thank you so much for the explanation. I need so much help from the hints and tutorial i feel like i didn’t actually ‘did’ it, more like just copying…really not the best feeling :frowning: but guess i will just need to work on it more and harder…

But thanks for the reply it helps a lot!

I feel like for this project, what i learned from the previous steps are not sufficient enough for me the construct out of my knowledge, a lot of helps needed…not sure whether it is just me or it is difficult for most people at this stage…

1 Like

may i know why? coz there is no ‘course’ before, why _ is needed even when it appears for the first time?

for the setter, why do we use the same ‘appetizers’, should we or can we use a different name? like newAppetizers etc?

set appetizers(appetizers) {
this._courses.appetizers = appetizers;

When you’re creating an object (/ class, later on) there’s a pattern the whole setter and getter thing follows. From what I’ve learned so far:

const menu = {
_courses: {
    appetizers: [],
    mains: [],
    desserts: []
  },

the object created here is what the getters and setters look for and interact with.
For every key: value pair in that object you’ll need a getter and a setter of its own (not sure about nesting them). In order to “populate” the values of the keys created here we use setters, in order to “view” the contents we use getters.

As the programmer, you could still access the initial object directly - but for you, the programmer, to know what not to directly access there is the “_” infront of it. This helps you when looking at the code after a while to easily understand what was going on, and any other programmer in the same way.

I wasn’t paying the “_” enough credit, though. Until I read Roy’s post, turns out the getters and setters won’t know which initial object (or it’s key: value pair) to refer to unless the objects have the preceding underscore to them.

So the underscore could be thought of as the “King-Maker”. :smiley:

Edit:

This appetizers can be anything, as long as you keep referring to that parameter, it could be
set appetizers(x) {
or
set appetizers(whateverYouWantToCallMe) {

I recommend using something short and close to the meaning of what it does / stores - duplicates like in your example do work but can make the whole thing confusing.

ah ok ok , so here if i say

set appetizers(x) {
this._courses.appetizers = x
}
like this?

i actually think duplication is confusing, but it is what is used in the hints and tutorial, that’s why i found it hard to understand.

1 Like

Recall that appetizers is an array. The setter must push to that array. An assignment will replace it with a non-array, single value.

But if the question is about the variable, then yes. Any variable will do. It’s handy to think in terms of singular and plural.

for (apple of apples)

or,

set appetizers(appetizer) {

}

If you’re going to use the setter then you’ll have to use the ambiguous syntax…

this._courses[courseName] = dish;
1 Like

Ok thanks!

I think i need some time to go over this…this subject is a bit hard for me …

1 Like

Yeah, I can confirm this task as well as whole object thing is much, much harder. My main problem is that my brain is trying to discard objects and implement this task through functions and arrays…
The problem is the object topic is explained as if it is as easy as for, while loops or functions topic - but it is not.
I guess I will have to read and practice more about objects to proceed, maybe I am too weak but my brain refuses to understand it from 1st (or even 2nd attempt) .
The good thing which I firmly believe is that this object topic as well as Classes (have no idea what’s that yet) is the cornerstone and hardest part to understand in JS - especially if you are a lawyer and have no prior experience in programming like myself :smiley: