What the Thermostat Output expected?

Tell us what’s happening:

please help me understand this :

  • The constructor accepts a Fahrenheit temperature. constructor(fahren).
  • create a getter and a setter in the class, to obtain the temperature in Celsius. set temperature(toFahren) , modify the temperature convert from celsius to fahrenheit.
  • Error : Thermostat should be able to be instantiated.

i only know that setter can modify the value that passed into, but in this case i don’t know what value would be passed into setter function.

thanks

Your code so far


// Only change code below this line
class Thermostat {
constructor(fahren) {
  this._fahren = fahren;
}
get temperature() {
  return this._fahren;
}
set temperature(toFahren){
  this._fahren = toFahren * 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
console.log(thermos.temperature); 78.8 in F

Your browser information:

User Agent is: Mozilla/5.0 (Android 8.0.0; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0.

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

Link to the challenge:

There is just one detail missed.

If the user types userObject.temperature() they won’t get Celsius but Fahrenheit. Try to return a Celsius T in that method.

thanks for the reply,

but still don’t get this.

i only take the example as a refference

class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}
const lol = new Book('anonymous');
console.log(lol.writer);  // anonymous
lol.writer = 'wut';
console.log(lol.writer);  // wut

with description

  • Getter functions are meant to simply return (get) the value.
  • Setter functions are meant to modify (set) the value.

still get the error thermostat instantiated when convert to celsius.

set temperature(toCelsius) {
   this._fahren = 5/9 * (toCelsius - 32);
}

Not sure if this will help. I imagine the thermostat as a real object. And someone reads the F temp therein, they put this value into a function. Then, they want the equivalent in a different scale. So you should use this._fahren=F to set the value, and a translation FC on the get

get temperature() {
return 5/9*(this._fahren - 32);
}
set temperature(F){
this._fahren = F ;
}

Does this make sense?


As for the error itself, I don’t think it’s a meaningful error. You’re instantiating the object, unless the console.log(object) is undefined.

Now, I can see you got the exercise, it’s just †messy wording† probably.

1 Like

sorry for my late reply

yes it work, maybe because i am confused with the description given, that get is simply return value, while set can modify value, but in your code it’s like get to modify value while set simply return value.

thanks

@sobadrdb@anon10002461 's answer is misleading. But so is the problem itself imo. get temperature() specifically RETURNS a Celsius-value temperature, and set temperature(temp) also specifically TAKES a Celsius-value temperature. So anon had the getter method right, but if you’re storing the value as _fahren the setter should be as follows:

set temperature(C){
this._fahren = ( (C * 9.0 )/ 5 ) + 32 ;
}

Basically, the reason you got that error message had to do with YOUR getter, as your setter function was correct! When you get temperature(), you have to return the value in Celsius. This is super unclear from the problem, and the error message is garbage, but that’s what is happening most likely. Anon did not test the code they suggested; if they had, it would not have worked.

1 Like