Use getters and setters to Control Access to an Object question

Use getters and setters to Control Access to an Object question
0

#1

Tell us what’s happening:
I’m not sure why my code is failing…I did some investigating around but I couldn’t figure it out.

Also, is “thermos.temperature = 26;” passing “26” as the parameters for the set method? If so, would it go through the formula?

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat {
    constructor(temperature) {
    this.temperature = temperature;
  }
    get temperature(){
    celcuisTemp = 5/9 * (this.temperature - 32)
    return this.celcuisTemp;
  }
    set temperature(celcuis){
    this.temperature = celcuis * 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
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 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


#2

When creating a new Thermostat, you pass the temp in Fahrenheit like:

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

Internally, you make it so when the getter is called, the temperature in Celsius is returned and also that the setter will allow you to set the temperature is Celsius.


#3

I understand that, but I’m comparing my solution to the answer and I don’t see the discrepancies (besides variable names). The specific error i’m getting is “Thermostat is not a constructor”. Is there syntax/rules I’m not following?


#4

You can not have a private property of the same name of your getter/setter. That is why you will typically see the following syntax (with the underscore).

    constructor(temperature) {
    this._temperature = temperature;
  }

#5

You have a few other issues also.

#1) The makeClass function should return Thermostat, but it does not.

#2) In your setter function, you refer to this.celcuisTemp, but you never defined a private property named celcuisTemp.

#3) The “use strict” setting forces you to always declare any new variable. You wrote the following without a declaration:

celcuisTemp = 5/9 * (this.temperature - 32)

#6

I figured it out, the return Thermostat; can’t be apart of class Thermostat brackets, just apart of function makeClass. One last question, what’s the significance of “const Thermostat = makeClass();” I’m not even sure why we need to create the class Thermostat within the function makeClass.

Thanks for your time, really appreciate it.


#7

You do not need it, but the way the tests are configured behind-the-scenes, I assume they need it structured this way.