Meal maker - getters and setters

Hey guys, I’m still having trouble with my setters.
I wrote and ran the program without a hitch and have also done my getters correctly, but now I’m stumped as to how we are meant to be using the setters.
if set appetizers(appetizerIn) { this._courses.appetizers = appetizerIn; } is the correct implementation, I have two issues.

  1. That looks to me like it will reassign a new value to the appetizers array rather than adding it to the array with .push(). Am I wrong here?

  2. How do we then assign new appetizers? Setters only accept one parameter, so do I need to build an appetizer object and use that as the argument when calling the setter method?

Thank you.

you are right, which means the setter is implemented correctly

simple assign multiple times:

menu.appetizers = 'dish_A';
menu.appetizers = 'dish_B';
menu.appetizers = 'dish_C';

if the setter is correctly implemented, this should push the three dishes into the array.

Thanks for getting back to me.

So in this instance ‘dish-A’ would be an object?:

menu.appetizers = { name: "soup", price: 5 };

I just don’t quite get how I add the second property (price);

this works:

menu.appetizers = { name: "soup", price: 5 };

an object is still one value. if you add a console.log() to your setter you can see that the whole object being passed to the appetizerIn parameter

I only asked as that is how I wrote the code.

My setter is wrote as follows:

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

I’m getting: TypeError: Cannot read property ‘price’ of undefined. This is at the stage where the bill (cheque) is devised by adding the prices of all three courses together. There was no error here prior to these new changes.

Do you HAVE to set a conditional in a setter before you can dictate what happens to the object?

If you need to see more of my code please let me know and thank you so much for your time and attention :slight_smile:

difficult to say without seeing the full code.

this still isn’t right, because now you will end up with:

appetizers = { name: "soup", price: 5 };

while you should have:

appetizers = [{ name: "soup", price: 5 }, {name: "pancakes", price: 8}];

the setter should push element into the array.

Ah, haha.

I raised this point early on in the discussion and I thought you told me that assignent rather than .push() was the correct way.

I’ll try again. Thanks again for your time :slight_smile:

Everything has worked perfectly straight away.

Shame about the confusion, but all is well that ends well :smiley:

So, whilst I was having trouble with my setters in “Meal Maker()” I moved on to other coursework.
I am now a little stuck with my setters on “Dog Maker()” as I am not expected to push anything into any kind of array, but instead make instances of a dog. Also, I would have to invoke three setters at once (name, breed and weight), so I’m confused there.

I’ve put my code bellow (minus the breed and weight properties so that it’s easier on the eye) . If someone could shed a light on how I write my code block for the setters it would be very much appreciated . . .

const dogFactory = (name, breed, weight) => {
  if (typeof name === "string" && typeof breed === "string" && typeof weight == "number") {
    return {
      _name: name,
       get name() {
         return this._name;
      },
      set name(nameIn) {
       //THIS IS WHERE I'M STRUGGLING AS THERE WILL BE THREE SETTERS TO INVOKE AT ONCE.
        this._name = nameIn;
      }
    }
  } else {
    console.log("Please enter valid format arguments");
  }
};

Did i say correctly? I meant incorrectly, that explains the miscommunication. I need coffee haha

For the dog factory, could you provide exercise url/link? Haven’t done that project in ages

Here’s the link to my/ the lesson. Let me know if it doesn’t work.

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/dog-factory

Again, cheers for your time.

why would you do this?

You create an object, the getters and setters come in to play after you created a dog instance with the factory

I think I’m going to need further intervention here.
My error is claiming laddy.breed() is not a function.

const dogFactory = (name, breed, weight) => {
  if (typeof name === "string" && typeof breed === "string" && typeof weight == "number") {
    return {
      _name: name,
       get name() {
         return this._name;
      },
      set name(nameIn) {
        this._name = nameIn;
      },
      _breed: breed,
       get breed() {
         return this._breed;
      },
      set breed(breedIn) {
        this._breed = breedIn;
      },
      _weight: weight,
      get weight() {
         return this._weight;
      },
      set weight(weightIn) {
        this._weight = weightIn;
      }
    };
  } else {
    console.log("Please enter valid format arguments");
  }
};

const laddy = dogFactory("Laddy", "German Shepherd", 42);
console.log(laddy);

laddy.breed("poodle");
console.log(laddy);

the error is right, according to the documentation:

The set syntax binds an object property to a function to be called when there is an attempt to set that property.

you don’t attempt to set/assign the property, instead, you attempt a function call. With setters, the call is done for you when you set/assign the property

I think I’m just totally lost at this point. I have written my setters properly, but I just don’t understand how to make a dog with them.

Please could you tell me how I can make the following dog using my setters:

name: “Laddy”,
breed: “German Shepherd”,
weight: 42

I’ve been stuck on trying to build a dog with setters for days now and I’m becoming disinterested and just need to move on. I’m sure I’ll understand as soon as I see the code.

Please and thank you :slight_smile:

its the factories job to make an object. The object creation by the factory should give the object getters and setters, so that we can use them to get or set the properties on the object later (after creation)

but that is not the setters job/responsibility. That is why you have a factory

a setter is for setting an individual property on an object

Hey again,

In my mental exhaustion I was simply using my setters incorrectly.

I understood that I was to use the factory to make instances of dog. However, when I went to change one of the properties of my instance of dog (laddy) it claims that there is no functionality to what I’m trying to change.

This was because I was trying to use my setter by applying laddy.breed("poodle") when I should have put laddy.breed = "poodle";

I just started studying for the first time since Friday and I’ve figured it out in all of about 30 seconds.
I should have just taken a break and come back to it, but trying to force through the issue I’ve just made it worse to the point where I’m coming across like a complete buffoon :laughing:

Thanks for your patience though :smiley: