Project Meal Maker - setters and push methods | the use of object literals versus arrays

Hi everyone,
Here is the link to this project
https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker

Also here is the link for something I previously posted about this project

I’ve been reviewing the exercises to Intro JS, and I’ve stumbled again with some doubts I hope this community can help clarify. Thank you all in advance! Let’s do this!

Doubts:
Like I had mentioned in my previous post about this project (link above) the getters and setters are not having an impact in generating a random meal… but I also now realize they don’t do much for the purpose of “setting” a new dish (?)

  1. Shouldn’t we add data (dishes) via the setters? It feels to me there is something confusing my brain :brain: :brain: that I cannot really put my finger on, an incongruence of sorts. Let me see if I can put some of this to words: it looks like we always want to couple a specific price to a specific dish when adding data. Because of that, we are leaving the setters out to dry, and they are not doing anything in terms of adding data:
  • a) shouldn’t we add data via our setter method, which should incorporate a price parameter?
  • b) or is my sense of confusion coming maybe from misunderstanding the role of a setter versus that of a method that pushes data? (and when would we use one or the other, for the purpose of adding data?)
  1. A final question: we decided to wrap the appetizers, mains, desserts, into a _courses object.
  • When do I know if I want to use curly braces or square brackets? I mean :face_with_monocle:, it seems to me objects and arrays are almost interchangeable – couldn’t we have also defined _courses as an array (of appetizers, mains, desserts), and couldn’t we have set appetizers, mains, desserts as objects (namely with a name and price) instead of arrays?
  • And if there is this interchangeability between arrays and objects, when do we decide to use one or the other?

Thanks for any ideas on this!
Pedro

I just went through the video and followed it through to get a working menu object.

What I find odd is that we created the three setter methods and then we don’t use them. My version of this code was way different and led me down a rabbit hole to try to utilize the setters, which resulted in non-standard code I may have alluded to in a previous discussion. We won’t even begin to discuss my approach.

The project as followed uses only the addDishToCourse method to push in new values. Even with the setters removed the code runs as expected.

1 Like

I guess I’ll have to give myself some more time to gradually start making more sense of things, I probably have some concepts clashing and this should clear out in the weeks of learning to come :slight_smile:

1 Like

I haven’t seen your code, but the lesson is simply demonstrating the various ways you can add data to your _courses object. You very well can add a dish to a specific course using the setter for that course, or you can use the addDishToCourse function to do the same. Two different ways to accomplish the same task. As far as whether to use an {object} or an [array], it could come down to preference, but an array can become extremely complex, and difficult to manage when you start nesting arrays inside of arrays. In the Meal Maker example, the ‘parent’ object is Menu. If we were making a real menu, courses would only be one of several nested objects. We might also have beverages, side dishes, etc. If we only used arrays, instead of menu._courses.desserts[3] to access the fourth dessert we would have menu[0][2][3] which could become really confusing.

Hi @midlindner, thanks for your feedback

With regards to the setter versus method:

The lesson seems to show but one way, not several. I now understand a bit what was troubling my mind, I’ll explain:

I’m new to programming right (1 month), so I begin to pick up on things …without necessarily understanding them fully… and this leaves a sense of something not being quite right across the board, but not knowing where to put my finger on it. I do now! :smiley:
I’ve detected an association between setting and adding values. Someone with programming experience can already foresee the rabbit hole this entails… but someone inexperienced as myself, simply relays a doubt that ends up stemming from the fact there are simply preferences (even, opposite views, if you roam other forums) on how to use a setter, specifically: what JS allows it to do versus how it reads and how one should use it.

I picked up om these signs in various ways. Not only the current project, but also on project Build a Library the video solution states on minute 7 “we’re using a method here instead of a setter because we aren’t assigning a value to the ratings property, we’re just changing the value that’s already stored at ratings”. That’s all well and fine :slight_smile:
It’s unavoidable though, to eventually come in contact with other code and coders that do things in various ways and have their own view of things. This is also part of the learning!

I guess I’ll forever remember the topic on getters, setters and methods to add data as my “welcome to the world of coding!”

Yes. I guess that will be the main take away!
I can also imagine: the way you foresee working with that data in the end, since I believe there is a difference in methods available to arrays and objects (?).

Thanks!
Pedro

Yes. What you intend to use the data for does make a big difference. Things will also become clearer as you learn about ‘classes.’ Technically everything, including arrays, are objects with their own properties and methods.

1 Like