Using getters and setters to control access to an object

I’ m just trying to se what’s still wrong with this code.

  **My code so far**

// Only change code below this line
class Thermostat {
constructor(fahrenheit){
  this.fahrenheit=fahrenheit;
}
}
get temperature() {
return 5/9*(this.fahrenheit - 32);
}
set temperature(celsius){
  5/9*(this.fahrenheit - 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 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

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

Link to the challenge:

Here you aren’t updating this.fahrenheit. The setter needs to update the internal data.

Thank you. I had to revisit the setter. But I still can’t understand what’s wrong. Here it is:

class Thermostat {
constructor(fahrenheit){
this.fahrenheit=fahrenheit;
}

get temperature() {
return 5/9 * (this.fahrenheit - 32);
}
set temperature(celsius){
this._celsius = 5/9 * (this.fahrenheit - 32);
}
}
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

console.log(temp)

You should only internally store the temperature in one way. Pick either Celsius or Fahrenheit. The getter/setter makes sure that the user gets/sets everything in Celsius no matter how you store it internally (after using Fahrenheit in the constructor)

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