Confused with 'this'

I know the correct solution is to change

this.fahrenheit = temperatureInCelcius;

into

this.fahrenheit = (celsius * 9.0) / 5 + 32;

But I don’t understand why. Why wouldn’t my code below work? Just want some explanation cause it feels like I am missing something.

Wouls greatly appreciate your help! <3

  **Your code so far**

// Only change code below this line

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

  get temperature(){
    const inCelcius = 5/9 * (this.fahrenheit - 32) ;
    return inCelcius;

  }

  set temperature (temperatureInCelcius){
    this.fahrenheit = temperatureInCelcius;
  }

}

// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
console.log(temp);
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
console.log(temp);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

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

Link to the challenge:

A temperature in F is not equal to a temperature in C - here you are missing a conversion

Thank you for your explanation!

Why is there a need to convert Fahrenheit into celsius? I thought that in the line

temp= thermos.temperature =26 

26 refers to 26 celsius? Shouldn’t it just set and update ‘this.fahrenheit’?
Like the example they gave on this:

class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}
const novel = new Book('anonymous');
console.log(novel.writer);
novel.writer = 'newAuthor';
console.log(novel.writer);

because you treat the stored as if it was in F

here you set the input F temperature as value of the property

here you are asked to return the value of the themperatre in C

here you set the received temperature (in C) in a variable that is treated as holding F

yes, but you are inputting the temperature in C in the thermostat
and the thermostat should also give back a temperature in C, it should be the same

with your code you do

thermos.temperature =26 
console.log(thermos.temperature) // -3.3333333
1 Like