Trouble with "getters" & "setters". unclear about this topic

Tell us what’s happening:
Describe your issue in detail here.

I am unclear and confused with getters and setters.
Here is what I understand in this assignment first get the value in Fahrenheit and get () convert it in Celcius scale. And set () set the new value updated and it is in Fahrenheit scale.
But what I didn’t get at all was getters and setters syntax! What should I name them and why do I need parameters in setters??
I am totally new with this.fahrenheit. why this is everywhere in this code instead of using constructor parameters fahrenheit ??

Please help me to understand…
Thanks

  **Your code so far**

// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
  this.fahrenheit = fahrenheit;
}
get () {
  let C = 5/9 * (this.fahrenheit - 32);
  return C;
}
set (Celsius) {
   this.fahrenheit = Celsius * 9.0 / 5 + 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

console.log(temp)
console.log(thermos)
  **Your browser information:**

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

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

Link to the challenge:

Pls check the implementation of getter and setter again in the example, you are missing property name for both.
Implementation in example:

// getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }

Implementation in your code:

get () {
  let C = 5/9 * (this.fahrenheit - 32);
  return C;
}
set (Celsius) {
   this.fahrenheit = Celsius * 9.0 / 5 + 32;
   
}

get temperature() {
let C = 5/9 * (this.fahrenheit - 32);
return C;
}
set temperature(Celsius) {
this.fahrenheit = Celsius * 9.0 / 5 + 32;

}

const thermos = new Thermostat(76);
let temp = thermos.temperature;

the temperature name used in let temp = thermos.temperature;
that’s ok?

Yes, it is correct, the code should pass now.

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