I need help understanding an issue in this specific example

class Thermostat {
constructor(fahrenheit) {
this._fahrenheit = fahrenheit;
}

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

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

This is a solution I got from the website after continuosly being so close to the soution but just couldn’t get it right.

Now, I understand in principle how it works, but what I simply cannot wrap my head around is why exactly do we convert the temperature back to Fahrenheit in the setter?
To my understanding, we are supposed to get the temperature in Celsius and also set it in Celsius.

What am I missing? Please help!

Because that’s the unit you are storing it in with the class variable this._fahrenheit. Would it make sense to store a Celsius value in that variable? If you did that, then when you called the getter you would still be doing the conversion on this._fahrenheit even though it was in Celsius and then your value would be way off.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.