Getters and Setters Exercise

I did the exercise, but I have a couple of doubts: (1) why can’t i just return the updatedTemperature in the setter? Why do I have to set the temperature to Fahrenheit? I understand that setter has to provide the temperature in Celsius, but isn’t the constructor taking the temperature in Fahrenheit only? (2) Why shouldn’t i use return in setter? The example did have it. I tried using it and gave me an answer of undefined. Not sure why.
Thanks so much for the help
Describe your issue in detail here.

  **Your code so far**

// Only change code below this line

class Thermostat {
  this.value = value;
get temperature(){
  return 5 / 9 * (this.value - 32);
set temperature(updatedTemperature){
  this.value = (updatedTemperature * 9.0) / 5 +32;
//return this.value;

// 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_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

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

Link to the challenge:

Well, first of all, I think that is generally not allowed in OOP.

Secondly, I’m not sure what it would mean for it to return a value:

thermos.temperature = 26

Here, you aren’t calling that function directly, You are setting the value of the property and the class/object is calling the setter. I think that that will just evaluate to 26 and whatever is returned will just be lost to the mists of time.

Why do I have to set the temperature to Fahrenheit?

Because that’s what we did here. It’s kind of a little weird function, but it is to show you that you can assign a value to the property and the setter intercepts that and changes the value that is assigned.

I understand that setter has to provide the temperature in Celsius, but isn’t the constructor taking the temperature in Fahrenheit only?

As I understand the instructions (and yes, this is a bit confusing), the constructor accepts F, the class stores the value as F, and the getter returns C and the setter accepts C. Why? Because the problem said so. Trust me, there will be many times in your coding career when you have to build something and you scratch your head at some of the choices.


Thank you so very much for this. I really appreciate it.

Yeah, I’m also struggling a bit with this one…having to put in 2 formulae for one answer, just doesn’t make sense to me, under “normal” circumstances one just needs ONE formula to calculate especially if a value is already given (in this case fahrenheit). I suppose this is something similar to “just learn parrot fashion and apply” kind of concepts
In high school I learnt about trig, was the VERY first time I had seen such “jargon”, it really didn’t make sense how it worked…logically so I decided “hell with this” (pardon the language) "I’m going to learn all this ‘parrot fashion’ ", so I did, I got 100% but don’t ask me what I learnt (applying in real world), years later as a tradesman I got to use it where it’s “supposed” to be used, where the decimal values are used via trig to convert to a real angle…still beats me (yes, I understand the routine of using trig with the decimal to convert to an angle, but looking at it LOGICALLY??? Sorry, I just use trig to convert to angles, that’s it lol), all I know is that one takes 2 values, divide the 2 and it gives an angle using trig
I’m talking about my experience and likening it here. I have an IDEA of how this getter/setter works (as the tutorial states, it’s the beginning of API which I understand) but using TWO formulae…to work out ONE thing??? lol, omw…this is where the saying comes in again “hell with it, I’ll learn it ‘parrot fashion’” and just “trust” it, so that’s how I’m going to learn it…PARROT fashion, let’s see where it takes me
UNLESS…someone can point me (link or what) to a proper logical explanation as to WHY…there’s 2 formulae instead of one, this I’d appreciate, but I suppose this is just how OOP works…run by “faith” I’d say

1 Like

just one thing Veronicarbulu…if I may chip in here a little, I was looking at that “return”, from what I understand it’s actually what’s been ASKED…when given a Fahrenheit value your class is SUPPOSED to “spit out” a Celsius value, the get method is there to give the user an answer and so “return’ing’” that answer to user, does this make sense? Hence the “return” and so you’ll find that within that formula it’s asking for Celsius, instead of using the word within the formula, the class is actually “returning” that answer to the user, hence the “return”. You’ll find that in JS the “return” is actually “giving back” to the user. Make sense? :wink:

1 Like

You can get away with only using one formula here, the one that converts from F to C. You don’t have to store the temp internally as F, you can store it as C if you like. There is a note about this in the instructions:

Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.”

In the constructor you can do the conversion from F to C and then the getter merely returns the internal temp and the setter merely assigns the value passed in to the internal temp.

1 Like

thank you, so what you’re saying then is there are 2 ways one can actually code this formula in, why then if this is the case use 2 formulae in same class? would you kindly show me how this would work in code please so I can analyze this code wise :wink:

I suggest you code it for yourself. I outlined how you would do it:

  • In the constructor, convert the temp you get in F to C and store it in a class property (such as this._temp).
  • In the getter, merely return this._temp.
  • In the setter, set this._temp to the value passed in (which is in C).

but then why use 2 formulae, surely there must be a reason for this somewhere along the line in JS :wink:

I want to make sure I am understanding your question correctly. Are these the two formulas you are talking about?

C = 5/9 * (F - 32) 
F = C * 9.0 / 5 + 32

If so, you don’t have to use them both, I just gave you a solution that only uses one of them. If someone wants to use both of them that’s just a personal choice. You can still get the right answer.

Does this answer your question?

yes to both formulae, and yes I know you gave the answer around using one formula, I’ll test and try it, thanks)
so why then does the answer give both formulae if a matter of choice. Your answer does help but it just means more code using 2 formulae…waste of time imo

Because whoever submitted that suggested answer decided to use both formulas. It is just a suggested answer. It isn’t the only answer.

I think this discussion is done. I’m not sure there is any more I can add. If you think using two formulas is a waste of time then that is fine, just use one. But the answer in the hints is perfectly acceptable.

Take care.

1 Like

thanks for the help)
and not trying to be rude, as it is…coders time is valuable, so why then make unnecessary code, it just wastes time if this makes sense? This is what I’m trying to point out. thank you anyway)

Right, remember that the suggested answers aren’t really curated by FCC. I find some of the suggested answers on some of the challenges to be bizarre choices. To be honest, I don’t look at them much, unless answering someone’s question about one. But it is useful to understand what they are doing, even if you don’t think it is the best approach. But as bbsmooth says, don’t assume that that is the only approach or even the best approach - it’s just a solution that one person came up with.


I like what you said about “not the only choice”, it tells me the flexibility of languages especially JS, there’s more than one way of doing something. For me that’s actually an eye-opener really…

1 Like

Yeeeeeeah man, after 2 days of…looking at this and recapping on getter/setter here, dissecting and analysing I have to update on this :wink:
I am proud to say that after a few attempts at using one formula instead of 2 I finally got the thing to work lol
But I’ve also found that removing the setter method from the class, the formula will still work?
And so I’m wondering now…if the setter method isn’t really used then for 2 formulae?
I thank you for your help :wink:

Well you still need both a getter and a setter method.

Internal temperature is Celsius:

  • constructor: stores T (converts T to Celsius first)
  • getter: return T (no conversion)
  • setter: set T (no conversion)

Internal temperature in Fahrenheit:

  • constructor: stores T (no conversion)
  • getter: return T (converts T to Celsius first)
  • setter: set T (converts T to Fahrenheit first)

I have to admit I’m not a huge fan of this challenge. It is supposed to demonstrate how you can have one internal temperature unit, while the getters/setters expose methods to interact with the thermostat in a different unit. But if the setter works with Celsius, why not make the internal temperature Celsius as well?

The only situation where this makes sense is when the person that sets up the thermostat with const thermos = new Thermostat(76) isn’t the same as the one who interacts with it via getters and setters. For example, when your thermostat is set up by an American who only knows Fahrenheit and who never touches it again, and the user of the thermostat is a European who only knows Celsius.

On second thought, despite this example being extremely contrived, maybe the challenge is not that nonsensical after all. I’m just not sure if it succeeds greatly in bringing the point across.

Yeah, some of the challenges are a little “artificial” in nature.

I can think of a case where you would want to store data in a format that the user never needs. For example, storing date/time stamps. You could store that as just the number as a private member but have a getter that return a localized string and setter that accepts either, converting it to the UTC code if needed.

But if people have suggestions about how to build a better challenge, then we can discuss it in the Contributors room. But I think there might be a little resistance to changing things that are (essentially) working. I don’t know, we’ll see.

Contributors room? Do you have a link or something?

TBQH I don’t really see more point to show with this challenge other than to convert , I’m not then sure what point they’re trying to get across?
The way I understand API is sending server info and then receiving back, imo that’s all API really does?
What I did was use the constructor property to install the formula and then output using getter, this way one wouldn’t need the setter method.

I also don’t really know what you mean by “T”?