Use getters and setters to Control Access to an Object** NEED HELP

Hi, I’m currently learning ES6 syntax but suddenly I find problems in “ES6: Use getters and setters to Control Access to an Object” section.
What’s wrong with my code
I don’t understand why but FCC console throws these errors:

// running test
Maximum call stack size exceeded
Maximum call stack size exceeded
Maximum call stack size exceeded
// tests completed

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat{
    constructor (fahrenheit){
      this.temperature=5/9*(fahrenheit-32);
    }
    get temperature(){
      return this.temperature;
    }
    set temperature(t){
      this.temperature=t;
    }
  }
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object

When you use getters and setters for a class, you can not have a property for **this (the class object) ** with the same name as the getter and setter function names.

4 Likes

After a while, I found the answer. My setter and getter functions have the same identifier as class properties “temperature”. That’s why the code cant executed.

function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat{
    constructor (fahrenheit){
      this.celsius=5/9*(fahrenheit-32);
    }
    get temperature(){
      return this.celsius;
    }
    set temperature(t){
      this.celsius=t;
    }
  }
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C
2 Likes

My solution is slightly different

function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor (fTemp) {
      this.temp = fTemp;
    }
    get temperature () {
      return (this.temp - 32) * 5 / 9;
    }
    set temperature (cTemp) {
      this.temp = cTemp * 9.0 / 5 + 32;
    }
  }
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C[spoiler]This text will be blurred[/spoiler]