Use getters and setters to Control Access to an Object Correction

Tell us what’s happening: I was getting confused with this sentence in the challenge “In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius”. I think it should be “… and a setter to set the temperature in Fahrenheit

**Your code so far**

// Only change code below this line

class Thermostat {
constructor(Fahrenheit) {
    this._Fahrenheit = Fahrenheit;
}

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

set temperature(Celsius) {
    this._Fahrenheit = (Celsius * 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
**Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15.

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

Link to the challenge:

The setter is in Celsius, which is to say that the argument is a temp in Celsius. Only the constructor is in Fahrenheit.

So, in thermos.temperature = 26; the value goes in Celsius to the setter and in the setter it transforms to Fahrenheit and it’s assigned to the constructor. Then when temp = thermos.temperature; you are calling the value in Fahrenheit in the constructor but the getter convert it in Celcius. (?)

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