Getters and Setters - thermostat exercise

Tell us what’s happening:
I’m a little confused. The question is asking me to obtain a temperature in Celsius when given a Fahrenheit value. I entered the conversion and the console.log shows that the temperature is successfully converted.

I am getting “X” for not defining a getter or setter - I’ve included both in the code;
and for the thermostat not setting the correct temperature - which console.log is showing correctly? I think.

I didn’t use the Fahrenheit conversion, I was thinking the question said we only needed to track in either C or F.
Here’s my error:

// running tests When instantiated with a Fahrenheit value,
Thermostat should set the correct temperature.
A getter should be defined. 
A setter should be defined. 
// tests complete

.
.
.

  **Your code so far**

// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
  this._fahrenheit = fahrenheit
}

// getter
get celsius() {
  celsius = 5 / 9 * (fahrenheit - 32);
  return this._fahrenheit;
}

// setter
set celsius(updatedTemperature) {
  this._fahrenheit = 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/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62

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

Link to the challenge:

The getters and setters need to be for the temperature

I’m getting there! I just don’t understand this error code:

“When instantiated with a Fahrenheit value, Thermostat should set the correct temperature.”

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this._fahrenheit = fahrenheit
  }

  // getter
  get temperature() {
    let celsius = 5 / 9 * (fahrenheit - 32);
    let fahrenheit = celsius * 9 / 5 + 32;
    return this._fahrenheit;
  }

  // setter
  set temperature(updatedTemperature) {
    this._fahrenheit = 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

You are keeping the Fahrenheit value internally but you getter needs to return a Celsius value.

I’m 100% sure you’re spelling this out for me, but I’m still struggling - I know this is in large part due to not understanding this concept well.

I changed both the getter and setter to celsius, but it’s still throwing the same error message. I feel like I need to put an "if temperature = fahrenheit { return temperature } type code in the getter area - but I think I’m WAY off the mark again.

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

  // getter
  get temperature() {
    let fahrenheit = celsius * 9 / 5 + 32;
    let celsius = 5 / 9 * (fahrenheit - 32);
    return this.celsius;

  }
  // setter
  set temperature(updatedTemperature) {
    this.celsius = 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

console.log(thermos.temperature)

Step back for a second.

The way you set this up, only data you know you have is this._fahrenheit.

Whenever you call the setter, you take in a Celsius value and must update this._fahrenheit.

Whenever you call the getter, you use this._fahrenheit and return a Celsius value.

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