Use getters and setters to Control Access to an Object test makes no sense

Tell us what’s happening:

Guys can you please explain this behavior to me, why this code I wrote passes the tests of this challenge, Its not a good test.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat {
  constructor(author) {
    this._Thermostat = Thermostat;
  }
  // getter
  get writer(){
    return this._author;
  }
  // setter
  set writer(updatedAuthor){
    this._author = updatedAuthor;
  }
}
  /* 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/73.0.3683.86 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

Well, the test is definitely incomplete. It tests that you’ve created a class using the class keyword, and that the class can be instantiated. It does let you set a property (not access a setter, in your case, but set a property) named temperature. And retrieve it. It does not check if there has been some “conversion magic” going on there, which is the point of the fahrenheit to centigrade thing.

Basically, if you added no code to that, and simply clicked test, it would pass. Yeah, I think that’s an issue. I might suggest you pop over to the freecodecamp github, and search for bugs related to this issue.

here is some other code I have that passes the tests and I also do not understand:

function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat {
constructor(inputF) {
this._f = inputF;
}
get f() {
return (5/9 * (this._f - 32));
}
set f(c) {
this._f = (c * 9.0 / 5 + 32);
}
}
/* 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 // where is this seen? 
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

  1. How to implement getters and setters with this class?
  2. Why does this not pass if I remove the _ character in all places before the f
  3. Why do I need getters and setters, I can just do regular dot notation, why is it better than just doing the below:
const community= {
  firstName: 'Free',
  lastName: 'Codecamp',
  fullName(){
    return `${community.firstName} ${community.lastName}`
  }
}
community.fullName= 'Stack Overflow' //setter
console.log(community.fullName);        //getter

So yes, as I said, the test is incomplete. They are looking for a getter and setter, with the name temperature. They state that the constructor should take the fahrenheit temp (which implies that the value would be stored in fahrenheit), then the getter and setter should take that value, and convert it to and from centigrade.

You have created a getter and setter, called f – so if you were to do:

const Thermostat = makeClass();
const thermos = new Thermostat(76); // sets the fahrenheit temp
let temp = thermos.f  // (you have created a getter called f(), which is accessed by .f)
thermos.f = 26;
temp = thermos.f        // (and here you'd get the result they expect in temperature)

But, as I said above, the test is incomplete. It doesn’t check if you have created getters or setters. The advantage to getters and setters? Occasionally, you want to alter the information before storing it. For example, suppose you wanted to take something into a getter, then fetch() something from the back-end to set the actual value to. There are many times where you want to mask the actual value from the user, to create it as “private”.