Use getters and setters to Control Access to an Object Error

Tell us what’s happening:
i got this error : " Calling the setter with a Celsius value should set the temperature"
i don’t understand it, and the value when using the getter and the setter look fine !? pls help thanks :smile:

  **Your code so far**

// Only change code below this line
class Thermostat {

// constructor
 constructor(tempF) {
   this._temperature=tempF;   }

// getter 

get temperature() {
let cTemp=((5/9)*(this._temperature-32));
return  cTemp;

set temperature(t) {
let cTemp=t*(9.0/5)+32;
this._temperature = cTemp;


// 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/99.0.4844.51 Safari/537.36

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

Link to the challenge:

You are running into the limitations of dealing with floating point numbers. Add the following to the end of the setter:

console.log('set temp to ', this._temperature);

Then remove the parens around 9.0/5 and see how the temp value changes. Apparently, forcing (9.0/5) to be calculated first is triggering this problem. In general, you should always round when doing division in order to handle this. It just so happens that if you remove the parens then you will get the exact answer without rounding for the values being tested.


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