"Use getters and setters to Control Access to an Object"

"Use getters and setters to Control Access to an Object"
0.0 0

#1

Tell us what’s happening:

I wonder whether this solution is really correct or not.
It passes the test but I want to know if it does what it is supposed to.
I know there is an issue with this particular challenge, that’s why I asked you guys to make sure the solution is correct. Thanks in advance.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */

class Thermostat{

constructor(fh){
  this._tmp=fh;
}

//getter
get cels(){
return 5/9*(this._tmp-32);
}

//setter
set cels(updTemp){
this._tmp=updTemp;
}

}
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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


#3

I hope I’ve got it right this time.

//getter
get temperature(){
return 5/9*(this._tmp-32);
//convert celsius
}

//setter
set temperature(c){
this._tmp=c * 9.0 / 5 + 32;
}

thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

I was missing a point here, I was not considering the fact that the setter (thermos.temperature = 26)
converted the temperature to fahrenheit (taking the 26 celsius as a parameter ) and getter (thermos.temperature) reconverted the result of the setter to celsius (26 celsius).

So both the parameter and the value returned from getter were celsius and had the same value 26.

It was a little bit confusing.