Instantiating a Class

Tell us what’s happening:
When I run the code, it says that I need to instantiate the Thermostat. How do I do that???

Your code so far


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

get temperature() {
  return (5/9) * (this.fahrenheit - 32);
}

set temperature(celsius) {
  this.fahrenheit = (celsius * 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

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

Link to the challenge:

You need to use this._fahrenheit when accessing the stored temperature variable. Whatever you called the variable in the constructor is what you need to call it when accessing the data.

1 Like

When defining the class you are using this._fahrenheit . Maintain it in the setter and getter. Instead of

return (5/9) * (this.fahrenheit - 32);

use

return (5/9) * (this._fahrenheit - 32);

Do the same for the setter.

1 Like