Hey folks ! I'm facing a problem in this code!

As you can see i wrote this code but when i ran it they tell me[Calling the setter with a Celsius value should set the temperature .]
can anyone explain it to me because i tried everything and when i type console.log(temp); the give result is always -3.333333
(1) why can’t i just return the updatedTemperature in the setter? Why do I have to set the temperature to Fahrenheit?
Your code so far

// Only change code below this line
class Thermostat{

get temperature(){
return 5/9*(this._fah - 32);
set temperature(updatedTemperature){
// 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

Challenge: Use getters and setters to Control Access to an Object

First of all, in the future, be sure to include a link to the challenge - we don’t have them all memorized. Or if you use the Get Help -> Ask for Help button, it will do it all for you.

To your problem…

Calling the setter with a Celsius value should set the temperature .

RIght. And this is your setter

set temperature(updatedTemperature){

Read the instructions:

Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.

OK, so you are storing it in F but your setter is accepting C. So, you cannot store that C value in your class without converting it, right? Right now you are storing the C value in the private variable. Since the getter is assuming that it is F, it is “converting a C value to a C value” and screwing it up. Presumably the test is passing a C value to the setter and seeing if it is getting the correct C value back from the getter. But it’s not because there is this metric/imperial mixup.

Does that makes sense?

When I fix that, the code passes for me.

1 Like

Well to be honest I still didn’t understand this part

Since the getter is assuming that it is F, it is “converting a C value to a C value” and screwing it up.

I tried many times to sit down and understand the logic of that challenge but each time i fail , Is there any other way or method to have the same result cuz this one really made me frustrated lol!


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:


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.


and a setter to set the temperature in Celsius.

This is where we run into a problem.

set temperature(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:

  1. You have to convert in the constructor so you can store it in C - then the setter and getter will work fine without conversion.
  2. 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.


Thank you so much @kevinSmith for spending time explaining that code to me!
I totally understood it ,
I appreciate the information and advice you have shared.

1 Like

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