# Can anyone explain me the working of this code?

Tell us what’s happening:

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

``````

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

I am not quite sure what you are asking here. Are you trying to understand what the code is doing?

Can you be more specific? What parts don’t make sense to you?

yes, i want to understand how the code is running.

I don’t understand the whole code.

Did the previous lesson make sense? (I’m trying to understand what you do know. If you are this far in the curriculum, you probably don’t need me to explain variable assignment )

1 Like

so in the line
`const thermos = new Thermostat(76);`
your’e creating new thermos object
after that
`thermos.temperature = 26; temp = thermos.temperature; // 26 in Celsius `
in these lines first your’e setting thermos objects temperature and then setting temp variable to thermos .temperature
There is 2 functions in the Thermostat class
first one says get so if you say
`temp = thermos.temperature`
something like this this function return the objects temperature
second one says so
`thermos.temperature = 26;`
this code sets objects temperature to 26
hope this helps

1 Like

yes they make sense but i don’t understand this lesson. Sometimes it gives output in Celsius and sometimes in Fahrenheit.

That’s the whole idea here. Internally the thermostat is Fahrenheit based on Fahrenheit the constructor uses Fahrenheit. However, the getter and setter functions provide access to the temperature in Celsius. The getter and setter make the conversion for you.

1 Like

Thermos object stores value as a fahrenheit
so when you say
`thermos.temperature = 26;`
youre sending celsius so it turns into fahrenheit
and in this part

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

it first turns fahrenheit into celcius and sends that back

1 Like

okay now i get it. Thankyou for your help

2 Likes