Use getters and setters to Control Access to an Objec

Tell us what’s happening:

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat{
  constructor(Fahrenheit){
    this._Fahrenheit = Fahrenheit;
  }
  
}
  /* 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 (Macintosh; Intel Mac OS X 10_14_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

great start ,you’ve made the constructor.

Now you need two more functions.
One to get the temperature and one to set it.
(but they both work in celsius so you will need to convert your _Fahrenheit value to return it as celsius )

Can you try and let us know what you are struggling with?

1 Like

Yes, I am not quite sure what the instructions are. am I supposed to create a method for Fahrenheit to Celsius and that method could also Celsius to Fahrenheit?
I am kind of lost in this question.

Start with something easy. Write the getter. It takes the internal value of _Fahrenheit and returns the equivalent Celsius temperature.

1 Like

So when a new Thermostat is declared (like the line shown in the challenge below), the temperature is specified in Fahrenheit. When thermos.temperature (the getter) is called, it should should return the temperature is Celsius. Also, If the setter is used as in thermos.temperature(0), the temperature argument should be Celsius, so that if the getter is used again, the same Celsius value of 0 would be returned. The key to solving this challenge is to figure out where you should make the temperature conversion. You have three choices, the constructor, the setter, or the getter. The conversion only needs to be done in one of these and not multiple ones.

const thermos = new Thermostat(76); // setting in Fahrenheit scale
1 Like