# Help understanding Getter/Setter a little more

Tell us what’s happening:

I was able to solve the problem laid out in this lesson with little issue, but I what I’m having trouble with is understanding the Set portion where celsius is converted to fahrenheit. I don’t know to get this program to work the other way around. I see how it works for F - C. I’m assuming that it does since “temperature()” is set to the conversion formula for C - F.

``````
// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
this.fahrenheit = fahrenheit;
}

get temperature() {
return (5 / 9) * (this.fahrenheit - 32);
}

set temperature(celsius) {
this.fahrenheit = (celsius * 9.0) / 5 + 32;
}
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36`.

The Thermostat is created with a temperature in Fahrenheit, so the “internal” temperature of `thermos` is stored in Fahrenheit.
The getter and setter methods however work in Celsius.
If you use `get temperature()`, it’ll convert the internal temperature from Fahrenheit to Celsius and return it.
If you use `set temperature(26)`, it’ll convert the Celsius value to Fahrenheit and store it in `this.fahrenheit`.

If you want to swap the units, the constructor would be called with a value in Celsius and store it in `this.celsius`, the getter would have to return a Fahrenheit value, and the setter should take a value in Fahrenheit, convert it and store it in `this.celsius`.

I’m honestly still lost. How do I call the constructor? Using `set temperature(26)` just throws an error. Would I also need to rewrite `this.celcius` into the program? To me, that would mean just rewritting the program to accept celsius instead of fahrenheit still leaving me in the same situation.

Constructor is a function that is used to set an initial value to the variables of class for example let say i have class `Car` which contains a variable `color` and i don’t know which color to choose right now i would rather choose it when i decide to actually make an object of Car. So i make a constructor to do this.

``````class Car {
var color;
constructor(choosenColor){
this.color=choosenColor;
}
}
``````

Now my class is ready now when i want to make an object of this class `Car` and i want it to be Black. Here’s what i’ll do:
`const myCoolCar = new Car("Black");`
This is how you call a constructor. A constructor gets called when you make an object of it.
So in the challenge this is where the constructor is getting called:

This makes an object `thermos` of class` Thermostat` and passes `76` to its `constructor` which just takes this value and give it to the variable `fahrenheit`.

Okay, this helps me understand better where everything is going. I added `this.celcius` to my code and it now displays both F and C in the console:

```get temperature() { return this.celsius = (5 / 9) * (this.fahrenheit - 32); }```

``` ```

```set temperature(celsius) { return this.fahrenheit = (celsius * 9.0) / 5 + 32; }```

`{ fahrenheit: 76, celsius: 24.444444444444446 }`

I can manipulate the values going from one to the other using:

//Fahrenheit to Celsius
```const thermos = new Thermostat(76); let temp = thermos.temperature;```

//Celisus to Fahrenheit
```thermos.temperature = 26; temp = thermos.temperature;```

I might have misunderstood you, but you said you’d like to “make the program work the other way around”. I assumed you’d like to make it so that the getters and setters work with Fahrenheit. There’s two ways to do that - it depends on which unit you’d like to use for the internal storage (stored in `this.fahrenheit` or `this.celsius`) of the temperature:

1. The constructor uses Celsius, the getters/setters work with Fahrenheit:
``````class Thermostat {
constructor(celsius) {
this.celsius = celsius;
}

get temperature() {
let fahrenheit =  (this.celsius * 9.0) / 5 + 32;
return fahrenheit;
}

set temperature(fahrenheit) {
this.celsius = (5 / 9) * (fahrenheit - 32);
}
}
``````
1. The constructor uses Fahrenheit, and the getters/setters work with Fahrenheit, too:
``````class Thermostat {
constructor(fahrenheit) {
this.fahrenheit = fahrenheit;
}

get temperature() {
return this.fahrenheit;
}

set temperature(fahrenheit) {
this.fahrenheit = fahrenheit
}
}
``````

Does that shed some light?

This did shed some light and I do have a better grasp at what’s going on.
I probably did ask the wrong question initially (trying to get better at asking more concise questions, I tend to ramble).

I figured what you were saying was to rewrite the program so that it accepts celsius to convert to fahrenheit. I should have asked if this particular program in the lesson was meant to work in both directions without rewriting.

I think the point of the exercise (or one point) to take away is this:

• the internal temperature value is in Fahrenheit, and stored in `this.fahrenheit`
• once the Thermostat is “created” with `let thermos = new Thermostat(fahrenheit)`, the interface that a user can interact with (=getters and setters) work entirely in Celsius

The point of the exercise is not to show how a unit converter Fahrenheit-Celsius (and backwards) would work. That’s something entirely different.

1 Like