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

Tell us what’s happening:
I’m struggling to figure out what’s not working here. How are you supposed to define the getter and setter? And why isn’t it finding the correct temperature?

Your code so far

// Only change code below this line
class Thermostat {
  constructor (fah) {
    this.fah = fah;
  }
  get (temperature) {
    return this._fah = cel * 9.0 / 5 + 32;
  }
  set (temperature) {
    this._cel = 5/9 *(fah - 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:

I changed a couple things but I still get the same errors. I removed the get parameter since it’s not needed, and I changed it so that it returns this._cel. For the setter, I set this._cel equal to temperature.

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

MDN has a bit more on the syntax if needed.

Okay so this is the closest I’ve gotten so far. It passes everything except setting the correct temperature for both and I’m not sure why.

// Only change code below this line
class Thermostat {
  constructor (fah) {
    this._fah = fah;
  }
  get temperature() {
    return this._cel = 5/9 *(thermos - 32);
  }
  set temperature(temp) {
    this._cel * 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

What is thermos here?

Your constructor sets _fah but your setter and getter use _cel. You need to pick one or the other

thermos is the variable that references the Fahrenheit value in the code that we’re not supposed to change.

And I had the constructor set the fah value because the instructions said to set the constructor to a Fahrenheit value. How do I have the constructor accept the Fahrenheit value while also having the getter and setter use _cel?

thermos is outside of the scope of the object definition. You should not try to make this only work for one specific instance of the Thermostat. You must delete the thermos here. Its a getter, so you shouldn’t change this._cel or this._fah at all, only use it to return back a Celsius temp.

With math converting between the two. There is only one temperature. It has two ways to store it, Fahrenheit or Celsius.

Okay I figured it out. My problem was that I thought I needed to return with this._cel instead of actually returning the conversion of this._fah in the formula.

However I still have a question about why this works.

The instructions say:
In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.
So how can the formula F = C * 9.0 / 5 + 32 be correct if that seems to be setting the Fahrenheit value?

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

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