Got no idea how this works can anyone explain me

Tell us what’s happening:

Your code so far


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

Your browser information:

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

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

Link to the challenge:

Since your question is quite broad, why don’t you tell us where are you having troubles? :slight_smile:

this getter and setter , just understood to write them but dont know what exactly they do

It’s pretty straightforward, they do what they claim to do…

This line calls the getter method, which does nothing but return the current temperature (converted to Celsius):

let temp = thermos.temperature; // 24.44 in Celsius

This line calls the setter method, which takes in a temperature (in Celsius) as argument, and sets the current temperature to its respective value in Fahrenheit:

thermos.temperature = 26;

The last line calls the getter method again:

temp = thermos.temperature; // 26 in Celsius

is there anyway to check its working because console.log return same value or its not complete yet

Could you rephrase that, I have no idea what you’re asking?

This two line should be enough in telling you that is actually working.

You are passing 76 as a value but getting 24.44 back.
If you actually console it:

const thermos = new Thermostat(76); 
console.log(thermos)
// Thermostat { f: 76 }
// temperatureis correctly initialized as 76

// but when I actually get it
console.log(thermos.temperature);
// 24.44

So I’d say it’s working: it’s converting from 76F to 24C without me explicitly telling it to do so: as the user of your class I’m not calling some method to convertToCelsius(), you are doing it for me :slight_smile:

Hope it make more sense now.

but when i console.log for thermos it is returning 78.8 and for thermos.temperature it is returning 84.4
dont know where are these values coming from
i didnt changed any value