ES6 Getter and Setter Issue

I believe I’ve done this correctly and followed the steps in the hint step by step. Can somebody tell me what I’m doing wrong please?

  **Your code so far**

// Only change code below this line
class Thermostat {
constructor(temperature) {
  this._temperature = temperature;
}
get temperature() {
  return (5/9*(this._temperature-32));
}
set temperature(newTemp) {
  _temperature = newTemp*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
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/94.0.4606.61 Safari/537.36

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

Link to the challenge:

Do you see any errors in the console below the code editor? You should be seeing one and it is telling you what the problem is.

Hello mate,

You’re nearly there, just look at what your setter function is doing and what’s being assigned the new temperature.

This is what I see

// running tests
When instantiated with a Fahrenheit value,
Thermostat should set the correct temperature.

A getter should be defined.

A setter should be defined. // tests completed

Am I misinterpreting what’s being asked for? The constructor takes a temperature parameter in Fahrenheit and sets the private variable to the same temperature. The getter returns the temperature converted to celcius. The setter takes a temperature in Celcius and sets the private variable to the that temperature converted to Fahrenheit. Is this not what’s being asked for?

This is what I see in the console when I paste your code into the challenge:

“ReferenceError: assignment to undeclared variable _temperature”

Look through your code to see how you are using _temperature. I think you will find that one of them is not the same as the others.

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