Use getters and setters to Control Access to an Object5684

Hello,
Im not sure if I am approaching this problem the proper way in my code however right now I am getting an error that my parameter in my constructor is not being defined.

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this._fahrenheit = fahrenheit;
  }
  get temperature() {
    return this._fahrenheit;
  }
  set temperature(fToC) {
    fToC = (5 / 9) * (fahrenheit - 32);
    this._fahrenheit = fToC;
  }
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

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

Link to the challenge:

fToC = (5 / 9) * (fahrenheit - 32);

What fahrenheit is this referencing? I don’t see it defined anywhere. I think it would help if you changed the name of the function arg from fToC to what it really represents. What are you passing into the setter?

Also, remember, the getter returns the temp in C. Seems a little weird you have it returning this._fahrenheit.

1 Like

@bbsmooth yes your right my apologies, my initial code really didnt make sense. I think this maybe a little better but now im getting error that fToC is not defined. fToC is my shorthand way to say fahrenheit to Celcius. Im not sure if I should be passing fahrenheit as parameter/argument for the getter function? also im trying to set variable fToC (fahrenheit to celcius) in the setter with the equation 5 / 9 * (fahrenheit - 32). but I am not doing it properly because im getting error that fToC is not defined. Ive tried let, const and var to create the fToC variable but i know im not doing this right. thanks for any help.

class Thermostat {

  constructor(fahrenheit) {
    this._fahrenheit = fahrenheit;
  }
  get temperature() {
    return fToC;
  }
  set temperature(fahrenheit) {
 const fToC = 5 / 9 * (fahrenheit - 32);
 
  }
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

it is not defined, where have you defined it in a way that the getter can access it?

From the instructions:

" In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius."

So this means you are passing a temp in C to the setter. I bet you can think of a better name than fToC :slight_smile:

im making alot of mistakes today, im a bit tired but I meant should I pass farenheit to the setter function

@bbsmooth and yes variable name celcius is better then fToC. Maybe fToC could be a good name for a function and not a variable

Internally, within your thermometer, you’ve created a property. Not a variable, but one property defined on the object created by new Thermometer(). That property is three only thing being shared between your getters and setters, and really any other functions you might define in your class.

Any variables in any of the functions defined in your class are only available in that function. So your getter can’t see the value you’ve created in your setter, as the variable is only scoped to that function.

So you want to set the value to a property in this. And you want your getter to retrieve that same property.

Your setter will have a variable, as will the constructor. These are used within that function to update some value in this.

1 Like

@ snowmonkey @ilenia @bbsmooth if I’m saying this correctly but now im trying to create a property within the Thermostat class outside of any function or constructor that way the getter and the setter can obtain that property? and yes your right both the constructor and the setter should share the same parameter, right? Do i need to use the keyword static?

something like this?

class Thermostat {
  static celcius;
  constructor(fTemp) {
    this.fTemp = fTemp;
  }

  set temperature(fTemp) {
    this.celcius = (5 / 9) * (fTemp - 32);
  }

  get temperature() {
    return this.celcius;
  }
}

you create this property this.fTemp but never use it again

this.celcius exists only after the setter is called, you can’t get in any way the original temperature value that was given when an instance of Thermostat was created

1 Like

A static property is one defined on the constructor’s prototype. What that means is, it’s a property shared by all instances of the class. They all can access methods and properties defined on the prototype - so in this case, if one sets the Celsius property on itself, it sets it for all Thermometer instances.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.