Getter, Setter, Constructor NIGHTMARES

Tell us what’s happening:

How’s it going my fellow learners? I’m hoping to gain some understanding in relation to getting and setting within ES6 and I’m hoping you can help me gain an understanding of what is supposed to be happening here.

Below is an example of what I’ve come up with after spending about 90 mins on this unit, I don’t really want to look at the solution just yet as I first want to understand why I don’t understand this unit as it could just be with how I’m processing the information.

As I understand it, getters allow access to retrieve an objects property value, whilst setters allows you to update said objects property value.

The class keyword to my understanding is like stating that an object can be used as a sort of template and the constructor keyword is to highlight an argument or passed in value, I could be totally wrong with my terminology and if so please let me know!

So I started off creating the class and constructor as instructed in the unit, then created a getter (declared celsius) and stored the fahrenheit to celsius formula within a const labelled ‘c’ as it’s a formula that is never going to change.

Then I’ve declared a setter with the same name as my getter (I don’t know if this is possible) where I feel like I’ve tried to return the outcome of the formula.

This doesn’t work and now mind mind has turned to clay so here I am.

My code so far

// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
  this._fahrenheit = fahrenheit;
get celsius(){
  const c = 5/9 * (fahrenheit -32);
  return c;
set celsius(c){
  this._c = 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


The errors I’m getting are:

// running tests

Thermostat should be able to be instantiated.

A getter should be defined.

A setter should be defined.

// tests completed

Your browser information:

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

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

Link to the challenge:

I think there are three things that will help you out.

  1. You set this._fahrenheit. You should use this._fahrenheit instead of fahrenheit.
  2. set celsius() needs to convert the temp to Fahrenheit so it can be stored into this._fahrenheit.
  3. Your getter and setter should be for temperature instead of celsius.
1 Like

Hey mate,

Thanks for this, I was able to solve the unit the other day and got so involved I forgot to come back to you.


1 Like