OK.
The way you have it set up, it is storing the values in F. That is fine. Note the instruction:
The constructor accepts a Fahrenheit temperature.
You’ve done that:
constructor(fah){
this._fah=fah;
}
You’ve chosen to store the temp as F. That is fine. You could have converted it to C, but that is a choice - either is fine.
Another instruction says:
In the class, create a getter
to obtain the temperature in Celsius
You’ve done that…
get temperature(){
return 5/9*(this._fah - 32);
}
You’re getter is supposed to return C and it knows that _fah is F so it does a little math to convert it. Good.
Next:
and a setter
to set the temperature in Celsius.
This is where we run into a problem.
set temperature(updatedTemperature){
this._fah=updatedTemperature;
}
The instructions say that the setter should accept C. But here you are storing whatever is passed onto _fah without any conversion.
Let’s say, I try to set body temp, traditionally 37 C or 98.6 F. If I use the setter, I call:
thermos.temperature = 37;
But the problem is that while the spec says that should be thought of as C, your setter is thinking of it as F and storing it on _fah without a conversion. So, when the getter is called, it assumes that _fah is in F and converts it to C, so it outputs it as 2.78, or whatever. It was expecting to get 37.
It’s kind of a weird little example, kind of artificial. Among other strangeness, the constructor accepts F and the setter and getter deal with C. There are two ways to handle this:
- You have to convert in the constructor so you can store it in C - then the setter and getter will work fine without conversion.
- You don’t do conversion in the constructor so you store it in F. But then, since the setter and getter deal with C, you will have to convert there.
I think in this case, option 1 is a slightly better option - you only have to convert once, in one spot - less chance for an error.
You have chosen option 2. That is fine, it can work just fine. But then you have to do conversions in the getter and setter. You have the correct conversion in the getter, but you are doing no conversion in the setter. You would need to take that C input and convert it to F before you store it on the private class member, _fah.
I was able to make that change to your setter and get it to pass for me.
Does that make sense?
Yes, the logic is a bit odd and having the constructor work in a different unit than the setter and getter is a bit odd. But sometimes you get that in dev work - you get specifications that you don’t think are ideal. Sometimes you think, “Why do they want it to work this way? Wouldn’t it be much better if I changed this?” Sometimes you can push back and change it. Sometimes you have no choice - you just have to do what you’re told.
Don’t stress it. Yeah, it is a bit illogical.