Confusing wording in ES6 challenge

Hello

The challenge wording says " Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.
In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius."

We construct this object with a temperature in Fahrenheit.

The wording asks then to return the temperature in Celsius with the getter (according to the Fahrenheit value used to create the object) and to set the temperature in Celsius with the setter however the solution asks for the setter to set the temperature in Fahrenheit which confused me a hell of a lot.

Is it me or the wording is incorrect here?

  **Your code so far**

// Only change code below this line
class Thermostat {
constructor(far) {
  this._far = far;
}
get temperature() {
  return (5/9) * (this._far - 32);
}
set temperature(c) {
  this._far = ((c * 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
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75

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

Link to the challenge:

were do you see this?

do you mean this?

the Thermostat class have to be consistent inside, so if the temperature is stored in fahrenhait the setter have to accept a celsius temperature and convert it to F before saving it

if the Thermostat class instead has the internal temperature saved in C, the getter and setter don’t need to make any conversion, but the contructor needs to convert the temperature

Okay let me re-explain my logic, the wording says “In the class, create a setter to set the temperature in Celsius.”

In the solution (the same as shown in my code) the setter takes a Celsius parameter which is then converted in Fahrenheit and stored as a property in the Thermostat object.

In other words it sets the temperature in Fahrenheit and not Celsius as worded in the exercise.

If it was worded “In the class, create a setter to set the temperature in Fahrenheit.” then it would make perfect sense with the expected solution.

Edit: And in the solution’s Hint, it is clearly written " 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."

To me, converts to F and set it to attribute means to set in F and not in C…

The internal implementation is a thing, what the setters and getters accept and give back an other. The setter sets a C temperature, so it accepts a temperature in C, the getter retrieves a temperature in C. And this is how it appears from the outside of the class.

The interal working can be in two ways: storing a C temperature or storing a F temperature, both work

The Hint, which is maintained by volunteers, shows only one of these approaches

1 Like

Thanks for your explanations, I get the wording now.

As you’re saying

this is how it appears from the outside of the class

It’s indeed worded from an outside perspective: the user sets the Thermostat in C but in the object it is converted into F.

The way I understood it was “set the Thermostat in C (input a C value) and sets the internal temperature in Celsius.” which did not make much sense to me.

Thanks again!

Though, you could rework your object to only store a C value. It would look exactly the same on the outside, but the inside would be different.

Yup that makes sense, reworked object using only C values:

class Thermostat {
  constructor(temp) {
    this._temp = temp;
  }
  get temperature() {
    return temp;
  }
  set temperature(c) {
    this._temp = c;
  }
}

That doesn’t quite work. The constructor takes a value in F

You’re right I omitted that the constructor takes a value in F, re-worked:

class Thermostat {
  constructor(temp) {
    this._temp = (5/9) * (temp - 32);
  }
  get temperature() {
    return temp;
  }
  set temperature(c) {
    this._temp = c;
  }
}
1 Like

Close. Minor typo here.

Oh I think I see it now, should it be:

get temperature() {
    return this._temp;
  }

We use ‘this’ to make sure the ‘temp’ var is the one pertaining to the object Thermostat and not a global one declared earlier, right?

1 Like

Yeah, you want the internally stored temp, otherwise you get some funky business with a closure.

1 Like