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

This code passes even though it’s incorrect from my research and not a solution provided. This is the second ES6 problem where the solution I’ve provided passes but, wasn’t correct. I’m not sure what I should do about this. It kinda worries me because, now I’m not sure if when I pass, if I’m actually doing it correctly. What are your thoughts? I only put it as a spoiler in case my understanding of the code being wrong is incorrect.

Your code so far

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

Challenge Information:

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

Your getter and setter methods are correctly converting temperatures between Fahrenheit and Celsius. The issue might be with the initial implementation, where the temperature is converted to Celsius in the getter and converted back to Fahrenheit in the setter. This back-and-forth conversion may not be necessary.

class Thermostat {
constructor(temperature) {
this._temperature = temperature;
}

// Getter
get temperature() {
return this._temperature;
}

// Setter
set temperature(updatedTemperature) {
this._temperature = updatedTemperature;
}

// Method to convert Fahrenheit to Celsius
toCelsius(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}

// Method to convert Celsius to Fahrenheit
toFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}
}

With this implementation, you store the temperature internally in whatever unit it’s provided (Fahrenheit or Celsius), and conversions are done only when necessary, not every time you access or modify the temperature.

why do you think your code is wrong?

From my research, it seems that I wasn’t supposed to modify the object within the get block before the return statement but, I could transform it within the return line. Hope that makes sense.

it’s not wrong, it works, doesn’t it?

It does work but, if I’m using something within the code incorrectly, I won’t have full understanding of the things within the problem and may continue to use it wrong moving forward.

You were working internally in Fahrenheit, that’s an acceptable approach

One thing I would improve in your code is avoiding overwriting the function parameters, but that can be solved by creating a different variable for the getter and setter

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