Getting massively confused with this task

Tell us what’s happening:

I’m very confused what they do mean with:
Now create a getter and a setter in the class, to obtain 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.

Like how? Do they want me to get the live temprature? Do they want to convert it? Do they that u give the temprature and then convert it? Do they just want to know the convertion from F to C? The more i read the more i confuse i get…

Your code so far


/* Alter code below this line */
class Thermostat{
constructor (temperature)
this_.temprature = temprature;
}
//getter
get Fahrenheit(-32){
return this._temprature;
}
//setter
set Fahrenheit(updatedTemperature) {
this._author = 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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15.

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object

yeah we can get the same result using normal functions.
but a difference between using a getter or setter and using a standard function is that getters/setters are automatically invoked on assignment. So it looks just like a normal property but behind the scenes you can have extra logic (or checks) to be run just before or after the assignment.

So if you decide to add this kind of extra logic to one of the existing object properties that is already being referenced, you can convert it to getter/setter style without altering the rest of the code that has access to that property.

1 Like

Hello KittyKora,

It looks like there are quite a few typos in your code. I would reset the problem and start fresh. Make sure that you are checking the spelling of your variables. For example, you have both temperature and temprature.

That being said, you have a few things correct. To start off, you do want a Thermostat class with a constructor, a getter, and a setter.

The part of the instructions that you seemed to miss (and I missed, when I was looking this over) is this:

Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.

Now create a getter and a setter in the class, to obtain the temperature in Celsius.

So the constructor will take Fahrenheit, the getter and setter use Celsius. Which means that you want to use the conversion formula in the getter and the setter.

3 Likes