Thermostat should be able to be instantiated

Tell us what’s happening:
The error message that I get when I don’t convert the temperature to Celsius doesn’t make sense. It says ‘Thermostat should be able to be instantiated.’ which it is already but not converted to Celsius

Your code so far


/* Alter code below this line */
class Thermostat{
constructor(temperature){
    this._temperature=(5/9)*(temperature-32);
}
get temperature(){
    return this._temperature
}
set temperature(updatedTemperature){
    this._temperature=updatedTemperature
}
}
/* Alter code above this line */

const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0.

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object

2 Likes

Your current code above passes when I run it. You might need to refresh the page and try running the tests again. Another approach would to have the getter doing the temp conversion, so the initialization of the class does not do any calculations until it is needed. Let’s say you had a different situation where the calculation takes time and it is possible that the getter might be rarely called. Also, let’s say you may have multiple instances of the class being created. You don’t want to make all those time intense calculations just to initiate them. Anyway, it is just food for thought.

1 Like

Had the same issue. I was storing the temperature in F and then while returning I was converting it into C.

The issue with my code was that I was not using parenthesis() appropriately. That lead to the issue.

So I changed my code from this.f = c * 9.0 / 5 + 32; to this.f = c * 9.0 / (5 + 32); and it started working.

But I still feel that the error message is misleading. It makes me think if there’s a problem with the constructor or something.

I’m getting this issue. I didn’t even realize I was supposed to convert to celsius. But this error doesn’t mention anything about the return values not adding up. Is says it cant instantiate the object, so whats up? Seems like theres a bug here, no?

// Only change code below this line
class Thermostat {
  constructor(temp){
    this._temperature = temp;
  }
  get temperature(){
    return this._temperature
  }
  set temperature(temp){
    this._temperature = temp
  }
}
// Only change code above this line

After I add the conversion to the getter and setter it passes. So I think this confirms that the error message is wrong.

The error message will also appear if your get/set functions are wrong.

It’s unclear from the problem statement (imo) but:

  • The constructor takes a Fahrenheit-value temperature
  • The getter returns a CELSIUS-value temperature
  • And strangely, the setter takes a CELSIUS-value temperature

I can’t imagine that it’s best practice for a class’s only setter to take a different value than the constructor and this unnecessarily confuses the question.

1 Like

In my opinion, the task description doesn’t match the requirements. It says:
" Note: When you implement this, you will track the temperature inside the class on one scale, either Fahrenheit or Celsius.", when apparently it actually wants us to develop an algorithm that converts from Fahrenheit to Celsius, lol.

I agree with you. Reaching for 20 chars.

The instructions say as much

Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.

Now create a getter and a setter in the class, to obtain the temperature in Celsius.

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

This is the power of a getter and a setter. You are creating an API for another user, who can get the correct result regardless of which one you track.

Right… but that is poorly written, and also inconsistent with how you would most likely write this in the real world. Why would the setter take a Celsius temperature, when the constructor takes one in Fahrenheit? That sentence should at least be reworded to say something like “Now create two class functions: a getter, to return a Celsius-value temperature; and a setter, which takes a Celsius-value temperature and sets the internal temperature accordingly.” The phrase “obtain the temperature in Celsius” is not clear in the context of the setter; for clarity it should be something like “take a Celsius temperature and set the internal temperature variable depending on your representation.”

I’m not complaining or anything, I really just need to submit a pull request someday to update the language.