Hello.
I have already searched the forum and couldn’t find it explained anywhere. If I missed it please direct me.
My code is passing alright, I also understand how getter() method is working, what I don’t understand is setter() method. Why this code:
set temperature(celsius) {
this._fahr = celsius * 9/5 + 32;
}
does not convert Celsius to Fahrenheit? Instead it is assigning value of celsius
to thermos.temperature
property which then gets passed to variable temp
which I think should be done by simply:
set temperature(celsius) {
this._fahr = celsius;
}
This code is passing, even though after execution it is passing to variable temp
negative value which is not what is expected.
Also when I went to check the guide for this challenge I found:
Hint 4:
Create a set method of the same name as the get method. It should have a parameter that accepts celsius temperature. Convert it to fahrenheit, and set it to the attribute.
Which imho means that I am supposed to convert Celsius to Fahrenheit and then pass it to object so it can be read.
What am I missing?
Your code so far
// Only change code below this line
class Thermostat{
constructor(fahr){
this._fahr = fahr;
}
//getter
get temperature(){
return (5/9) * (this._fahr - 32);
}
//setter
set temperature(celsius) {
this._fahr = celsius * 9/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
console.log(temp);
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
console.log(temp);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
.
Challenge: Use getters and setters to Control Access to an Object
Link to the challenge: