Hi!, I need some help with getters and setters method

class Thermostat {

  constructor(F = (C * 9.0 / 5 + 32)) {

     this.thermos = F;

  }

  get temperature() {

    return this.thermos;

  }

  set temperature(C) {

    this.thermos = C;

  }

}

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

The code above is my and the one below is the exercise.

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.

Remember that `C = 5/9 * (F - 32)` and `F = C * 9.0 / 5 + 32` , where `F` is the value of temperature in Fahrenheit, and `C` is the value of the same temperature in Celsius.

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

This doesn’t work the way you want it to. You need to change the temp to C in the body of the constructor, not in the parameter list.

Actually, you are trying to change it to F, which you don’t need to because the constructor accepts a temp in F. But you can definitely change it to C before you store it in this.thermos.

1 Like

Thank you so much, man.
You are amazing thank you, I have solved it.

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