Possible typo in the exercise instructions

Hello, in the ES6 challenge for ‘getters and setters’, there are instructions saying :

“In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.”

Unless I’m misunderstanding, I believe the setter should be set to Fahrenheit, not Celsius. So the text should read:

“In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in/to Fahrenheit.”

Looking at the solution code, it appears that converting from Celsius to Fahrenheit is what should be happening.

This challenge was also very tricky. I wish it had a longer explanation on how to work with getters and setters. I had to look up other videos on the subject.

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

Link to the challenge:

There is no typo. The setter accepts a temperature in Celsius. The getter returns a temperature in Celsius. The way the temperature is stored internally is not specified. You can use whichever temperature scale you prefer.

I tried to submit the exercise using the following code but couldn’t get one of the tests to pass.

class Thermostat {
  constructor(celsius) {
    this.celsius = celsius;
  }
  
  get temperature() {
    return (this.celsius * 9.0) / 5 + 32;
  }
  
  set temperature(fahrenheit) {
    this.celsius = (5 / 9) * (fahrenheit - 32);
  }
}

  const thermos = new Thermostat(44); // in celsius
  console.log(thermos);
  let temp = thermos.temperature; // fahrenheit value
  console.log(temp);
  thermos.temperature = 76;
  temp = thermos.temperature; // is this a setter?
  console.log(temp);

I cant get the following test to pass:

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

I get the hunch that my setter code is wrong somehow since the value does not change from Fahrenheit to Celsius.

Lets look back at the original code:

// Only change code below this line

// 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

In this code,

this line is calling the constructor:

const thermos = new Thermostat(76); // Setting in Fahrenheit scale

this line is calling the getter

let temp = thermos.temperature; // 24.44 in Celsius

and this line is calling the setter

thermos.temperature = 26;

So, the constructor should accept a temperature in Fahrenheit, but yours uses Celsius:

  constructor(celsius) {
    // ...
  }

And the setter should accept a temperature in Celsius, but yours uses Fahrenheit:

  set temperature(fahrenheit) {
    // ....
  }

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