ES6 - Use getters and setters to Control Access to an Object

Tell us what’s happening:
Hi all. Thank you in advance for looking at this.

I just need a quick sanity check on this code please. It is passing all tests except the last one, " Calling the setter with a Celsius value should set the temperature ." I am getting the correct values in the console.log. I have looked at the example solutions, and can’t tell how mine is different.

Your code so far

// Only change code below this line
class Thermostat  {
  constructor(temp) {
    // Temp entered in Fahrenheit.
    this._temp = temp;
  }
  get temperature() {
    // Return in Celcius.
    return 5.0 / 9.0 * (this._temp - 32.0);
  }
  set temperature(value) {
    // Convert from Fahrenheit to Celcius.
    this._temp = 9.0 / 5.0 * value + 32.0;
  }
}

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

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

Link to the challenge:

But aren’t you storing this._temp in Fahrenheit in the constructor? Why would you want to convert it to Celsius here?

Oh wait, your comment is wrong. You are converting from C to F here.

The issue here has to do with floating point operations like division. Do a console.log of this._temp at the end of the setter and you will see what I mean. If you follow the formula given and move value to the beginning of the equation then it will work properly.

1 Like

Thank you for your response! Yes, I wrote the comment in the code incorrectly. Okay, I was wondering if this was a floating point issue. Is this an issue with the test, as surely multiplication should be communitive?

The instructions could probably be clearer and tell you to round to a certain number of decimals (such as 1). But you also share some responsibility here because your code is producing 78.80000000000001 for the answer, when the answer should be 78.8. Because there are known issues with using floating point operations then you have to take that into account in your code. You would normally do this by rounding.

So ultimately, the responsibility is on you to make sure you account for irregularities with floating point operations.

1 Like

Given that this rather pedantic issue is irrelevant to the lesson at hand, I would suggest a simple ‘safe floating point’ check in the vein of, ‘abs(a - b) < tol’ to check for equality.

Ya, I agree, this floating point issue should be avoided. My suggested solution would be to choose values for the test that give the same result regardless of the order the operations are performed.

1 Like

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