Ambiguity in ES6: classes : getters/setters wanted?

Tell us what’s happening:
I can solve this problem, but I wanted to point out some ambiguity in the description of the problem. It says:

Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.

When implementing with an internal Fahrenheit-scale, the tests fail.
I’m suggesting to reflect on the simplicity of this exercise - having two functions for both temperatures might be less ambivalent. :slight_smile:

Your code so far


// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
  this._temperature = 5/9 * (fahrenheit - 32);
}

set temperature(celsius) {
  this._temperature = celsius;
}

get temperature() {
  return this._temperature
}
}
// 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36.

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

Link to the challenge:

you can use what scale you want inside the class, but the constructor needs to accept a temperature un F, the setter in C, and the getter needs to give back a temperature in C

what’s your code with an internal Fahrenheit scale?

1 Like

Ok, I get your point. It wasn’t clear to me that the getter should return a temperature in Celsius - maybe I need to read the text and not just the table at the bottom… :slight_smile:

Functioning code is:

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this._temperature = fahrenheit;
  }

  set temperature(celsius) {
    this._temperature = celsius * 9.0 / 5 + 32;
  }

  get temperature() {
    return 5/9 * (this._temperature - 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

Thanks and have a lovely day!
Sebastian.

It’s been a while but I remember when I was working on this one I was also confused by some wordings. Can’t remember exactly what though.

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