The constructor accepts a Fahrenheit temperature. constructor(fahren).
create a getter and a setter in the class, to obtain the temperature in Celsius. set temperature(toFahren) , modify the temperature convert from celsius to fahrenheit.
Error : Thermostat should be able to be instantiated.
i only know that setter can modify the value that passed into, but in this case i don’t know what value would be passed into setter function.
thanks
Your code so far
// Only change code below this line
class Thermostat {
constructor(fahren) {
this._fahren = fahren;
}
get temperature() {
return this._fahren;
}
set temperature(toFahren){
this._fahren = toFahren * 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(thermos.temperature); 78.8 in F
Your browser information:
User Agent is: Mozilla/5.0 (Android 8.0.0; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0.
Challenge: Use getters and setters to Control Access to an Object
Not sure if this will help. I imagine the thermostat as a real object. And someone reads the F temp therein, they put this value into a function. Then, they want the equivalent in a different scale. So you should use this._fahren=F to set the value, and a translation FC on the get
get temperature() {
return 5/9*(this._fahren - 32);
}
set temperature(F){
this._fahren = F ;
}
Does this make sense?
As for the error itself, I don’t think it’s a meaningful error. You’re instantiating the object, unless the console.log(object) is undefined.
Now, I can see you got the exercise, it’s just †messy wording† probably.
yes it work, maybe because i am confused with the description given, that get is simply return value, while set can modify value, but in your code it’s like get to modify value while set simply return value.
@sobadrdb – @anon10002461 's answer is misleading. But so is the problem itself imo. get temperature() specifically RETURNS a Celsius-value temperature, and set temperature(temp) also specifically TAKES a Celsius-value temperature. So anon had the getter method right, but if you’re storing the value as _fahren the setter should be as follows:
Basically, the reason you got that error message had to do with YOUR getter, as your setter function was correct! When you get temperature(), you have to return the value in Celsius. This is super unclear from the problem, and the error message is garbage, but that’s what is happening most likely. Anon did not test the code they suggested; if they had, it would not have worked.