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

Hello,
I have passed this challenge by just writing a class. I don’t seem to get it why just writing a class passes this challenge.

Thanks!

  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor(fTemperature) {
      this.fTemperature = fTemperature;
    }
  }
  /* 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

you didn’t post a link to the challenge but if I recall correctly, this is just the first exercise of a series that will teach you about inheritance. So keep going to get the whole picture.

The way you wrote a class, is the new ES6 way of writing classes in JavaScript.

Also I think it should be:

class Thermostat {
    constructor(fTemperature) {
      this.temperature = fTemperature;
    }
  }

The way you did it, you created a Thermostat Class with two variables, “temperature” (26) and “fTemperature” (76). I guess this is the strong and weak side of the language itself. You can create whatever you want outside any class…

1 Like

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

ok I understand your question now. Yes it would seem that there should be code to check for the getter and setter which was requested by the challenge but right now the challenge is doing just a class check.
You could open a github issue against freecodecamp for this. I believe the link is:

1 Like
function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat{
    constructor(fahrenheit){
      this.celsius=5/9*(fahrenheit-32);
    }
    get temperature(){
      return this.celsius;
    }
    set temperature(updateTemp){
      this.celsius=updateTemp;
    }
  }
  /* 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
1 Like

wow, how did you come up with that?

@ilenia I’m sorry. I’m reading your answer right now, i just want to help him. I see that i cannot ankswer anymore any post, i just wanted to help and learn.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.