Tell us what’s happening:
Since the pass criteria for this exercise doesn’t include coding the setters and getters I thought I’d post my full code here to see if the way I’ve set up the function is correct.
Unlike the example code, since the input is in Fahrenheit and I want the output in Celsius, I put the formula for Fahrenheit to Celsius in the constructor object block.
The get code returns the input (in Fahrenheit) as degrees in Celsius, while the set code returns the input as it is (since the exercise implies the input is already in Celsius).
I tried this code out in CodePen and it seems to do the things I intend but I’m not sure if this is what the exercise is asking. Please inform.
Your code so far
function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat {
constructor (Fahrenheit) {
this._Fahrenheit = 5/9 * (Fahrenheit - 32);
}
get temperature () {
return this._Fahrenheit;
}
set temperature (x) {
this._Fahrenheit = x;
}
}
/* 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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 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/