Tell us what’s happening:
I can solve this problem, but I wanted to point out some ambiguity in the description of the problem. It says:
Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.
When implementing with an internal Fahrenheit-scale, the tests fail.
I’m suggesting to reflect on the simplicity of this exercise - having two functions for both temperatures might be less ambivalent.
Your code so far
// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
this._temperature = 5/9 * (fahrenheit - 32);
}
set temperature(celsius) {
this._temperature = celsius;
}
get temperature() {
return this._temperature
}
}
// 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/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36.
Challenge: Use getters and setters to Control Access to an Object
you can use what scale you want inside the class, but the constructor needs to accept a temperature un F, the setter in C, and the getter needs to give back a temperature in C
what’s your code with an internal Fahrenheit scale?
Ok, I get your point. It wasn’t clear to me that the getter should return a temperature in Celsius - maybe I need to read the text and not just the table at the bottom…
Functioning code is:
// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
this._temperature = fahrenheit;
}
set temperature(celsius) {
this._temperature = celsius * 9.0 / 5 + 32;
}
get temperature() {
return 5/9 * (this._temperature - 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