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

Im struggling with figuring out whats going on here and why. This challenge wants us to GET and SET the temperature in celsius but, wants us to use different calculations for the get and set functions? I dont understand.

I also dont understand why we use “return” for GET but not SET.

And I havent been able to figure out how to write a log for this challenge.

If I can get help with those questions, I’ll just ignore why my algebra equations were rejected, even though I know they were valid.

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  get temperature() {
    return (this.fahrenheit - 32) * 5 / 9;
  }
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9) / 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 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

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

Link to the challenge:

The code you posted above is correct so I’m assuming your equations were different in some way? It would help if you gave us your actual code, even if it does not work, so we can see what you did. We don’t need you to paste in the solution code since we can see that for ourselves if we want :slight_smile:

1 Like

Because the getter is actually returning a value that we can use in other code. The setter on the other hand is only modifying an internal class variable. There is nothing to return.

1 Like

Why would we use one equation to get a value but a different equation to set the exact same value?

You need to look at the type of value you are storing and returning.

In the solution you posted above, you are storing the temp in F internally. So any time you change that internal value you need to make sure you are storing it in F. The setter takes a value in C, so you can’t just store that value in this.fahrenheit, you need to convert it to F first.

Likewise, the getter returns a temp in C. So you need to convert the internal temp from F to C so you can return the correct value.

Classes are an encapsulation of a concept.
kind of like skin encapsulates your organs/blood/bones
The outer layer presents something different than the internals.
The class has inner-workings that it doesn’t want to reveal to everyone so it has setters and getters that hide what is actually happening within.

For eg. The class may be an “american” one and stores all its values in Fahrenheit. But in order for people in other countries to use it, it needs to allow them to see the temperature in Celsius. So it implements a getter in order to show the temp in the desired unit without revealing that it is actually only ever measuring in Fahrenheit.
The setter is the same. It hides the fact that the temperature is stored in Fahrenheit because the people using it only know about Celsius.

This is an API (Application Programming Interface) and it is something you will see many many many more times so try to get a handle on it in this simple form.

Thanks. I am repeating every section and challenge as many times as it takes for me to understand the concepts well enough to explain it to someone else. I will probably revisit this thread again in the very near future.

1 Like

This challenge is still not making sense to me. How do I log it so I can see what is going on?

instead of returning the values right away, declare a variable inside the getter for eg and set it to the value you believe it should return
then log that variable before returning it

I know you didn’t ask but just in case it helps, here’s a break down of this class as I see it.

class Thermostat is a class that stores the temperature value in Fahrenheit.
(it is constructed with a Fahrenheit value so it is easiest to just store that value as-is)
The class implements one getter and one setter.
The getter reads the stored temperature in Fahrenheit and converts it to celsius and returns that value.
The setter is given a value in celsius which it converts to Fahrenheit and stores it.

1 Like

Wdym set it to the value I think it should return? Isnt the whole point of this challenge to convert an input value?

you were asking how to log I believe?
so I was suggesting a method of logging.
For eg.

You could log this method this way:

  get temperature() {
    let v = (this.fahrenheit - 32) * 5 / 9;
    console.log(v);
    return v;
  }

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