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

Tell us what’s happening:
Following is my code but I do not know what is wrong. I check with a hint but the way I understand is that we have to convert the Fahrenheit to celsius, in the hint, the code is the other way around and I am really confused.

  **Your code so far**
// Only change code below this line
class Thermostat {
constructor(fh){
  this._fn = fh
}
set Temp(value){
  return this._fn = 5/9 * (value - 32);
}
get Temp(){
  return this._fn * 9.0 / 5+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
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

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

Link to the challenge:

Try describing this code. Begin like this:
I have created a class called Thermostat that…

I have created a class whose constructor takes an in value, the constructor has a property which is private _fn which is equal to the value passed as a argument.

Next, I am setting the temperature by set method, which takes in a value as an argument, the function returns the converted Fahrenheit to celsius,

Next, I am getting the temperature by get method but in get method, I am converting the Fahrenheit to celsius.

Try this instead:

You have created a class whose constructor takes a ??? value.

What did you want to say here?

Edit: Try to figure out what the inputs are. The constructor take a temperature in ??? unit. But the setter takes a temperature in ??? unit.
(They are different. So the equation used should take that into account)

Does the get get a value as an argument as well? I thought get is just grabbing the value from private property and then print it out.

Getter is not taking any input (just the setter).
However, the units matter still…

make sure to use the required name for setter and getter, look at the code already written at the bottom


@hbar1st in this case I think your suggestions are not hitting the mark for this reason

2 Likes

I am still very confused but got many of them right, following is my code

// Only change code below this line
class Thermostat{
  constructor(value){
    this._temp = value
  }
  get temperature(){
    return 5/9 * (this._temp - 32)
  }
  set temperature(value){
    return this._temp =  5/9 * (value - 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

The confusion in the question is

In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.
Remember that C = 5/9 * (F - 32) and F = C * 9.0 / 5 + 32 , where F is the value of temperature in Fahrenheit, and C is the value of the same temperature in Celsius.

From my understanding, it is asking

  • get value is celsius. Which I did and is right
  • set temperature to celsius. (here is confusion. To get the temperature in celsius we need same formula as i used in get because C = 5/9 * (F - 32) but that is formula used in get

I hope I make sense

I’m not sure that you have understood the problem perfectly. (or at least that is what I suspect).

The internal representation of the temperature is one thing (fahrenheit or celsius, it is up to you). But depending on that decision, it will affect how the setter and getter work.

That is because if you choose to store the value in celsius initially (by making the constructor do the conversion) then the setter/getter do not need to convert it again. (we are told for eg that the setter input is a celsius unit, and the getter output is celsius as well).

So if the internal representation of the temperature is celsius, the only conversion that happens will happen in the constructor.

BUT, if the internal representation is fahrenheit.
Then you must do a conversion in both the setter and the getter as we are told that the setter only accepts celsius values and the getter only returns celsius results.

In your specific case, your constructor is taking the input value and storing it as-is.
That means your internal representation is fahrenheit.

It also means that everytime someone calls get you must convert the internal value to celsius.

And everytime someone calls set, you must convert the given input (unit celsius) to the internal fahrenheit representation to store it.

Final issue then is, which equation does what conversion?

The equation that starts with F= should be used in your setter.
(right now you are using the conversion to celsius in your setter even though your internal representation is Fahrenheit)

The equation that starts with C= should be used in your getter.
(again you are using the wrong one there)

I realize that this may leave you further confused.
I’m sorry but it is just a combination of things going on here.
1- understand the math (the equations given)
2- understand the internal representation of the temperature in your class
3- understand the requirements of the inputs and outputs for the setters/getters

You will need to understand all 3 points.

I have added comments of my confusion now that i got getter working

  get temperature(){
    //get value in celsius
    //meaning the value given to us e.g. 76 in in F
    //we use the following formula to covert F into C
    return this._temp = 5/9 * (this._temp - 32)
  }
  set temperature(value){
    //set value in celsius
    //the following code does set the temp is C, but 
    //why is not working n seem get and set have same
    return this._temp =  5/9 * (value - 32)
  }

the getter should not change your internally saved temperature.
The temperatre should stay the same
image

I am still not sure if I get it, my code now

  get temperature(){
    //get value in celsius
    //meaning the value given to us e.g. 76 in in F
    //we use the following formula to covert F into C
    return this._temp;
  }
  set temperature(value){
    //set value in celsius
    //the following code does set the temp is C, but 
    //why is not working n seem get and set have same
    return this._temp =  5/9 * (value - 32)
  }

sorry not to be able to understand and I really want to.

can you please post all your code?

and please also tell me if you have decided to store the temperature inside the Thermostat in Celsius or in Fahrenheit

The temperature is in Fahrenheit as it is when Initialized (I didn’t touch that)

// Only change code below this line
class Thermostat{
  constructor(value){
    this._temp = value
  }
  get temperature(){
    //get value in celsius
    //meaning the value given to us e.g. 76 in in F
    //we use the following formula to covert F into C
    return this._temp //= 5/9 * (this._temp - 32)
  }
  set temperature(value){
    //set value in celsius
    //the following code does set the temp is C, but 
    //why is not working n seem get and set have same
    return this._temp =  5/9 * (value - 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

you need to output the value in Celsius, but you don’t have to change the stored temperature. Right now you are outputting in Fahrenheit

I was doing this before
return this._temp = 5/9 * (this._temp - 32)
that is coverting into Celsius or I am wrong again?

what is an equal sign used for in javascript?
for eg.

let thisVar = 3;

What’s happening here?

(if you know the answer, then looking at your return statement, what does it do other than return?)

assignment, thisVar will be equal to 3

return statement will return this._temp - 32 and then multiply the result with 5/9

yes that is quite correct. So then, if you see what you said earlier

This return statement is actually doing 2 things, no?
(There’s an equal sign in there)

Just return the converted value without changing the internal value.
(that’s what getters are for, to return data in different formats but without actually modifying the internal representation of the data)

This part is confusing. so when the getter don’t convert the value then where is value being converted?