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

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

I do not know how to set the temperature in Celsius

/* Alter code below this line */

class Thermostat{
        
        constructor (temperature){
        
        this._temperature=temperature;

        }
    get temperatureUser (){

        return this._temperature;
    }
      set temperatureUser (updatedTemperature){
          
          this._temperature=updatedTemperature;
                           
                        updatedTemperature=(5/9)*(temperature-32);
      }
}


/* Alter code above this line */

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

The constructor for this class takes a Fahrenheit temperature, but the getter/setter always return/take Celsius. If you are going to store _temperature as F in the constructor, then you have to convert _temperature to C in the getter before returning it. Likewise, you have to convert updatedTemperature to F in the setter before saving it to _temperature.

No, it did not work :frowning:

/* Alter code below this line */

class Thermostat{
        
        constructor (temperature){
        
        this._temperature=temperature;

        }
    get temperatureUser (){

        return this._temperature;
    }
      set temperatureUser (updatedTemperature){
          
            updatedTemperature=(5/9)*(temperature-32);

          this._temperature=updatedTemperature;
                           
                      
      }
}


/* Alter code above this line */

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

The formula you are using in the setter is for converting from F to C. Since the setter takes a C and you are storing _temperature as F, you want the formula for C to F instead.

One thing that might help you keep this straight is to name your function parameters appropriately. For example:

set temperature(celsius) { ... }

Also, just noticed this, the names of your setter and getter are not correct. If you want to use a setter to set the ‘temperature’ then your setter has to be named ‘temperature’ as well.

1 Like

Thanks, I will try it

But it does not still work. :frowning:

/* Alter code below this line */
class Thermostat{
      
      constructor(F){
          this._F=F;
      }
          //getter
   get writer(){

       return this._F;


   }

          //setter
       set writer (C){
             
             C= 5/9 * (F - 32);

           
       }         
}


/* Alter code above this line */

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 getter/setter names are still not correct. Remember, if you want to set/get the ‘temperature’ then your getter/setter have to be named ‘temperature’. The line:

thermos.temperature = 26;

is calling the setter, so it is looking for a setter with the name ‘temperature’.

The objective of your setter is to set the variable this._F. The setter takes in a temperature in C and then must convert that temperature to F before assigning it to this._F. Your setter currently does not update this._F. Also, you are still using the formula for F to C.

1 Like

I wll try it again. Thanks!

I read that Thermostat shoul be instantiated. What is that?

/* Alter code below this line */
class Thermostat{
     constructor(F){
         this._F=F;
     }
     //getter
   get temperature(){
       return this._F;
   }
     //setter
   set  temperature (C=5/9*(F-32)){

         this._F =C
   } 
}


/* Alter code above this line */

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

Don’t worry about the ‘instantiated’ error you are getting right now. Once you fix the remaining issues in your class that error will disappear.

Remember, your getter is supposed to return a value in Celsius, so you can’t simply return this._F since that is in Fahrenheit, you have to convert it to Celsius first.

Also, you can’t do the conversion in the function parameter list in setter. Move your conversion logic to the body of the function and make sure you are using the correct formula.

I have done it. Thanks!

/* Alter code below this line */
class Thermostat{
     constructor(F){
         this._F=F;
     }
     //getter
   get temperature(){
       return (5/9* (this._F-32));
   }
     //setter
   set  temperature (C=5/9*(F-32)){

         this._F =C
         
   } 
}


/* Alter code above this line */

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


So I hate to rain on your parade, but you still don’t quite have it right. I know the challenge is telling you it is correct, but that is a bug in the challenge. Copy/paste your above code back into the challenge and add the following at the very bottom:

console.log(temp);

You’ll see that the value of temp is not 26.

The problem is in your setter. The value being passed in is in Celsius and you must convert that value to Fahrenheit before saving it to this._F. So you need to use the formula for converting from Celsius to Fahrenheit and you need to do that conversion in the body of the function.

I will do it. Thanks!