Trouble with Getters and Setters challenge ES6

Tell us what’s happening:

Hello everyone! I’m having some problems with the challenge, every test except 1 have passed with the error being When instantiated with a Fahrenheit value, Thermostat should set the correct temperature. I’m also quite confused over the concept of a getter and setter so any tips or explanations are welcome. Thanks!

BTW I’ll be inactive from 1:00PM to 2:50PM so if you don’t get a response during those hours that’s why, Appreciate the help!

  **Your code so far**

// Only change code below this line
class Thermostat {
contructor(temperature) {
  this.Fahrenheit = Fahrenheit;
}
//getter
get temperature() {
return 5/9 * (this.Fahrenheit - 32);
}
//setter
set temperature (Celsius) {
this.Fahrenheit = Celsius * 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45.

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

Link to the challenge:

Hello!
Try this:

class Thermostat {
constructor(temperature) {
  this.temperature = temperature;
}
//getter
get temperature() {
return 5/9 * (this.temperature - 32);
}
//setter
set temperature(Celsius) {
this.temperature = Celsius * 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;

You spelled “constructor” incorrectly.

Too lazy to type it out again, so just giving you a link

ok thanks @bbsmooth

The link should help

Thanks @Hjb1694 ! LOL

Those typos are crazy aren’t they, btw I thought it was standard procedure to not name the getters and setters by the same name as the constructor. Could you explain why not having different names for the getter, setter, and constructor works?

Also, although I appreciate the solution it’s best practice on the forum to guide person asking a question(me in this case) to an answer instead of just giving it to them, it’s also best practice to wrap any full solution to challenges from the curriculum in <spoiler></spoiler> tags.

Best,
Cy499_Studios

Object keys have to be unique. If there is a property called “foo”, you can’t also have another property called “foo” (which a getter/setter is, it’s basically a special kind of object property/function). And constructor is just what constructs the object, it’s always present and it would not make any sense to allow you to call it what you want (if you called it something different, how would the language know how to construct the object?)

1 Like

other than the spelling issue, where does Fahrenheit comes from? isn’t the name of the parameter temperature?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Ok so here’s my updated code:

// Only change code below this line
class Thermostat {
contructor(temperature) {
  this.temperature = temperature;
}
//getter
get temperature() {
return 5/9 * (this.temperature - 32);
}
//setter
set temperature (Celsius) {
this.temperature = Celsius * 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

But I’m getting an error that says RangeError: Maximum call stack size exceeded

What does this mean and why does my code still not pass?

Never mind I figured it out (forgot the _ in the variable name LOL) thank you everyone for all your help!

[spoiler]

// Only change code below this line
class Thermostat {
constructor(temperature) {
this._temperature = temperature;
}
//getter
get temperature() {
return 5/9 * (this._temperature - 32);
}
//setter
set temperature (Celsius) {
this._temperature = Celsius * 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[/spoiler]

1 Like