Tell us what’s happening:
The following code I have input clears the challenge, however I would like to understand the functionality of the Thermostat. Up until this point, I have been able to pass arguments into a function in order to test it and better understand it, but with this I don’t know where to begin.
This is my understanding, which I am hoping you can verify or correct:
I created a class named Thermostat with the constructor Fahrenheit. The value of Fahrenheit is itself. My getter is designed to get the Fahrenheit value, whatever that is, and set that as the temp.
My setter is designed to take Fahrenheit input and convert it into Celsius.
I know that temp is an object and thermos is an object that has the property temperature.
I don’t understand whether it is necessary to specify “Celsius”, whether my constructor and getter statements are redundant and so unnecessary, or whether the stated variable temp had to be used in my answer
Your code so far
function makeClass() {
"use strict";
class Thermostat {
constructor (Fahrenheit){
this._temperature = Fahrenheit;
}
get thermos(){
this._temperature = temp
}
set thermos(Celsius){
this._temperature = 5/9 * (Fahrenheit - 32);
}
}
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.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
First of all, you should know that this challenge is a bogus. It doesn’t test the validity of your getter and setter or the class itself.
All you really need to pass is this code
function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat {
constructor() {
}
}
/* Alter code above this line */
return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
The test only tests that portion of the code.
As you can clearly see, you can’t check your understanding regarding getter and setter with this challenge.
We must also settle problems within your code:
- The challenge expects the name of your getter and setter to be
temperature
.
- You did not design your setter to take
Fahrenheit
value as an input. The name of the parameter you wrote for it clearly indicates Celsius
.
- The
Fahrenheit
in the setter is undefined
all the time.
Now back to your question
- You don’t need to specify “Celsius”. If you do, it tells other programmers that your function expects Celsius value. Name is just a name, it is only meaningful to programmers not the program.
- Your constructor lets you initialize instance variables in your object, so it is not redundant.
- Getter is redundant in this case and also wrong. The
_
prefixed variables are private variables by convention. Providing the getter for that private variable makes it public by convention, which is a contradiction.
-
temp
variable is not needed.
1 Like
Thank you. I spent some time searching the forums and also discovered that another FCC’er had made a guide on the ES6 challenges. His explanation on classes, setters, and getters were much clearer than the challenge and really helped me understand them better. I also reworked my code and was able to create a “working thermostat” that gave the right feedback when checking it in the console in Chrome.
function makeClass() {
"use strict";
class Thermostat {
constructor (Fahrenheit){
this._tmp = Fahrenheit
}
set temperature(F){
this._tmp = (5/9 * (F - 32));
}
get temperature(){
return this._tmp;
}
}
return Thermostat;
};
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature;
thermos.temperature = 100
temp = thermos.temperature;
console.log(thermos);
I was trying to explain to someone else what they were doing wrong earlier today, and the solution here still doesn’t quite make sense. In addition to what @gunhoo93 says, if you bother to write the getters/setters, the conversion has to happen either:
- in the getter, not the setter (you input Fahrenheit, the underlying private variable is F, and you get Celsius every time)
- in the constructor as well as the setter (input is Fahrenheit, the underlying private variable is Celsius, and you get C every time)
Otherwise it’s even more nonsensical than it already is: when you instantiate the class, _temp
is a value in Fahrenheit. If you get
immediately, the value is therefore in F. But if you set an F value, then _temp
is now in C.
Today is the first time I’ve really looked at this challenge in detail, and the more I look at it, the less sense it makes. Plus, I don’t understand what the hell it’s doing in the ES6 section (bar the fact it uses class
) when there’s a perfectly good OO section on FCC, and getters/setters have been in the language for years.
(N.B. I know several issues have gone into GH about this, and that moving all the challenges to a new repo is blocking a helluva lot of work at present [or was until extremely recently? Haven’t checked for a week or so])
Could you please give me the link to the guide you mentioned from another FCC’er? I’m also having trouble with ES6 an could use some clearer explanations. Thank you!