Converting Celsius to Fahrenheit

Hello.
I have already searched the forum and couldn’t find it explained anywhere. If I missed it please direct me.

My code is passing alright, I also understand how getter() method is working, what I don’t understand is setter() method. Why this code:

set temperature(celsius) {
        this._fahr = celsius * 9/5 + 32;
    }

does not convert Celsius to Fahrenheit? Instead it is assigning value of celsius to thermos.temperature property which then gets passed to variable temp

which I think should be done by simply:

set temperature(celsius) {
        this._fahr = celsius;
    }

This code is passing, even though after execution it is passing to variable temp negative value which is not what is expected.
Also when I went to check the guide for this challenge I found:

Hint 4:

Create a set method of the same name as the get method. It should have a parameter that accepts celsius temperature. Convert it to fahrenheit, and set it to the attribute.

Which imho means that I am supposed to convert Celsius to Fahrenheit and then pass it to object so it can be read.

What am I missing?






Your code so far


// Only change code below this line
class Thermostat{
constructor(fahr){
  this._fahr = fahr;
}
//getter
get temperature(){
  return  (5/9) * (this._fahr - 32);
}
//setter
set temperature(celsius) {
      this._fahr = celsius * 9/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
console.log(temp);

thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
console.log(temp);

Your browser information:

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

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

Link to the challenge:

If you are given a temperature in Celsius, you need to perform a conversion to get the correct value in Fahrenheit. Let’s say that I, an American, am speaking to someone in Scotland. They tell me that it’s currently 0 degrees. To know how cold that is, I have to convert it to 32 degrees Fahrenheit. I look at my thermostat and see that it is 57, so I convert it to 14 degrees Celsius before I reply.

Conversion is performed here, right?

Right. That sets the internal Fahrenheit value based on a Celsius input.

But when I perform those instructions, value logged to the console is still 26 (in Celsius), and it should be 78.8 (in Fahrenheit). It looks like conversion is not performed correctly.

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

thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
console.log(temp); //26 in Celsius

The class always takes and returns the temperature in Celsius. It gets stored internally in Fahrenheit, similar to how I might mentally convert to Fahrenheit when talking to a European but use the Celsius units in conversation.

If you add a console.log(this._fahr) as the last line in your setter, you’ll see that it is being correctly converted internally.

1 Like

How can I know what is the value which is returned from setter? I didn’t see it in JS documentation.

I see it know. thermos._fahr is the one which gets changed, not the temperature parameter.

And after I logged thermos_fahr it returned correct value of 78.8

Thank you @ArielLeslie

Good job on working through it until it made sense.