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

Having a little trouble inserting formula into the getter. How do I calculate something for C in the getter when I declare C in the setter? So it’s showing C as not defined.

Your code so far


// Only change code below this line
class Thermostat {
constructor(F) {
  this._F=F;
}
get temperature() {
 
  return this._F;
  
}
set temperature(C) {
  this._F=C;
  
}
}
// 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

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

Link to the challenge:

First thing you need to do is decide which temperature scale you are going to store the temp internally in the class (either F or C). Currently you are storing it as F in the constructor (saving it to _F). If you want to continue doing this then you need to do the proper conversions in both the getter and setter.

For the getter you need to convert the value in _F to C before you return it. In other words, you can’t just return _F because that is stored in F and the getter should return the temp in C.

For the setter you need to convert the value passed into the setter from C to F before you save it to _F.

The challenge gives you the two formulas you need to do these conversions. You use those formulas in the setter/getter just like you would use them in any other function. For example, a simple function to convert F to C would be:

function convertToCelsius(F) {
  let C = 5/9 * (F - 32);
  return C;
}

The getter is basically doing the same thing except that instead of passing F into the getter you are using the value stored in _F.

// Only change code below this line

class Thermostat {

constructor(F) {

this._F=F;

}

get temperature() {

C = 5/9 * (_F - 32)

return this._F;

}

set temperature© {

F = C * 9.0 / 5 + 32

this._F=C;

}

}

// 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

So I tried inserting the formulas a bunch of different ways including return statements on both the getter and setter which I assumed was wrong, then using keyword let on the getter formula, like let C = 5/9 * (_F - 32). I also assumed that was wrong. The methods I try keep throwing either _F isn’t defined or C isn’t defined… I switched the formulas around from the getter and setter thinking I used the wrong one, still nothing. I understand what I have to do with the formulas, It’s applying them into the getter and setter is what I’m having difficulty with.

Look again at my example function convertToCelsius. Do you understand what is happening in that function? If not, then I think you need to go back and redo the JS basics and forget about more advanced ES6 features for now. If you do understand then you should realize that that function is basically what you need to do in the getter with a slight modification.

get doesn’t allow arguments, because it 's supposed to retrieve info? So where do I pass the F like you passed in that function ? You are setting C with let to do things to the passed F argument and returning the final value of C based on what the “formula” did with C & F. If you suggest I go back to Basic, what part of it specifically? It is a slight modification of your example which apparently flew past me, so I will RP and hope to get fresh opinions. I appreciate the explanation though!

You are correct, you don’t pass an argument to the getter because you already have that value, you stored it in the constructor

constructor(F) {
  this._F=F;
}

Do you understand what is happening in the constructor? When you instantiate an object of this class:

const thermos = new Thermostat(76); 

76 is being passed into the constructor (as F). Look what you are doing with the value passed into the constructor. You are saving it to a class member (this._F). Now the getter and setter can convert that class member value to Celsius.

There are two formulas in the challenge description. Do I use both, one, or use one of each in the getter and setter? Get doesn’t allow any arguments to pass in so whatever C/F I use is undefined. Can someone specifically point out the lines where I went wrong and make suggestions without giving me the answer (which sounds ridiculous but idk). It’s been day 2 on this challenge for me and I have read like 70 forum posts on this challenge. I have tried putting in the formulas differently and rearranging the returns and assignments but it’s to no avail. Thanks!


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


}
set temperature(C) {
  this._F=C;
  
}
}
// 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

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

Link to the challenge:

This will be my last attempt and then I’ll let someone else take over.

The constructor is called when you create an object for the class. You pass a temperature value in Fahrenheit to the constructor and in the constructor you save that value off in the class member variable this._F. That class member variable is then available for you to use in the getter/setter for the class. I think maybe this is the concept you are stuck on. When you put this. before the variable name then it means that it belongs to the class and can be used in any method of the class (which includes getters/setters) just like you would use a variable passed into a function.

The getter returns the value saved off in this._F but it has to convert it to Celsius first. So you can use the formula to convert from F to C to get the value of this._F in Celsius and then return that Celsius value. Right now you have:

return C = 5/9 * (F - 32)

The good news is that you are using the correct formula for converting from F to C here. Two things that need to be fixed:

  • You are trying to return an assignment statement. Instead, you need to return the temperature value in Celsius. There should be no equals sign in your return statement. Look at the convertToCelsius function I gave you above. You see how I did the assignment first to get the conversion to Celsius and then returned the value on the next line. I suggest you use that approach here.
  • The variable F does not exist in the body of the getter (because as you noted above, nothing is being passed into the getter). But the class member variable that you set in the constructor does exist in the body of the getter (and setter, and any other method in the class). This is the value that you want to convert to Celsius.

For the setter, you are saving the value passed in to the class member variable this._F, which is exactly what you want to do, but since you have decided to store the value for this._F in Fahrenheit you first need to convert the value passed into the setter to Fahrenheit before you save it to this._F.

I hope this is enough to help you figure this challenge out.

2 Likes

It worked! Just woke up and seen your reply and applied the separate return statement and used the class member variable inside the formula within get. I assumed that this._F=F would set F as this._F for me to readily use as F. I didn’t know that after I save the F into this._F that I would have to use this._F to reference F in the code. So I kept thinking (F-32) was correct because it technically is this._F because I set F to _F. I believe I even tried to do the same method in my previous attempts but was returning _F and not using the “let” declaration. I’m gonna do this challenge a few more times over and watch videos/use other resources to fully grasp what is happening. But your multiple explanations did the trick lol, I appreciate it!

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

Congrats, I’m glad you got it working. Concerning your statement above, assignment always assigns the value on the right of the equals sign to the variable on the left of the equals sign. It’s clear with a simple assignment like

num = 5;

that the variable num is assigned the value 5. If you replace the number 5 with a variable then the right side of the equals sign is evaluated first to get the value of the variable and then that value is assigned to the variable on the left. In the constructor

this._F=F;

you have a variable on the right (F). The value held in F is passed into the constructor when the object is created:

const thermos = new Thermostat(76);

So in this case F will equal 76. So 76 is assigned to the variable this._F which you are then free to use in any method in the class.

1 Like