I thought I have the get and set methods defined, but I actually got it wrong, and it says that I don’t have it defined. Could you please assist me on where the error is?
Your code so far
// Only change code below this line
class Thermostat {
constructor(tempFahrenheit) {
this._tempFahrenheit = tempFahrenheit;
}
get tempCelsius() {
return this._tempCelsius;
}
set tempCelsius(tempFahrenheit) {
this._tempCelsius = 5/9 * (tempFahrenheit - 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
ES6 - Use getters and setters to Control Access to an Object
I changed the name for the setter and getter to be ‘temperature.’ Here’s my edited code:
class Thermostat {
constructor(temperature) {
this._temperature = temperature;
}
get temperature() {
return this._temperature;
}
set temperature(temperature) {
this._temperature = 5/9 * (temperature - 32);
}
}
‘When instantiated with a Fahrenheit value, Thermostat should set the correct temperature’ and ‘Calling the setter with a Celsius value should set the temperature’ are the feedback messages that need to be fixed. I’d like to know where I’m going wrong.