ES6 - Use getters and setters to Control Access to an Object

I thought I followed the instructions, but I’m still missing hitting 2 out of the 7 checks for completion.
I’m missing:
When instantiated with a Fahrenheit value, Thermostat should set the correct temperature .
And
Calling the setter with a Celsius value should set the temperature .

I need help please in thinking this out to answer it myself.
I tried to add basic comments to accomplish following the parameters in solving them.
Thanks in advance,
Shawn Wright

// the following is a class called Thermostat
class Thermostat {
  // following is the costructor that accepts a Fahrenheit temp
  constructor(F) {
    this._F = F;
  }
  // following is my getter which obtains temp in Celsius 
  get temperature() {
    return this._C;
  }
  // following is my setter which sets the temp in Celsius 
  set temperature(F)  {
    this._C = 5/9 * (F - 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/109.0.0.0 Safari/537.36

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

Link to the challenge:

Are you sure you want to have two internal variables, this._F and this._C?

Let’s look at what the first two lines of code do.

const thermos = new Thermostat(76);

This calls the constructor and sets this._F to 76.

let temp = thermos.temperature;

This calls the getter, which returns this._C. But what is the value of this._C at this point?

1 Like

Y: This calls the getter, which returns this._C . But what is the value of this._C at this point?
M: I think if I’m following you, it would be 76, which is incorrect.

Sorry I’m really trying to think this out. I’m sure I’m stupid, so I appreciate your patience with me.

Would it be 76? Where was this._C set to 76? Definitely, this._F was set to 76 in the constructor. But I’m having a hard time finding where this._C was set to anything at this point.

But since you have this._F set to 76, don’t you think it might make sense to use that variable to help you return a value from the getter?

1 Like
// the following is a class called Thermostat
class Thermostat {
  // following is the costructor that accepts a Fahrenheit temp
  constructor(F) {
    this._F = F;
  }
  // following is my getter which obtains temp in Celsius 
  get temperature() {
    return this._F = C * 9.0 / 5 + 32;
  }
  // following is my setter which sets the temp in Celsius 
  set temperature(C)  {
    this._F = C;
  }
}
// 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

I’m not sure if my getter is correct now according to what I think you were trying to help me with, but if so, my setter must be off still, because now I’m getting:
error of:

ReferenceError: C is not defined

Where is C defined here? You aren’t passing it into the getter like you do with the setter. It isn’t a global or class variable. So I don’t think you can use C here.

I’ll give you a hint. there should be no equals sign in your return statement. You don’t need to initialize a value here. You need to convert a value before you return it.

If the setter accepts a temp in C then should you save it to a variable that should be in F? I think you might need to do a conversion first.

1 Like

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