Use getters and setters to Control Access to an Object [Berlin]

Tell us what’s happening:
why this?
Cannot set property temperature of #<Thermostat> which has only a getter

Cannot set property temperature of #<Thermostat> which has only a getter

Cannot set property temperature of #<Thermostat> which has only a getter

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat {
  constructor(pharma) {
    this.pharma = pharma;
  }
  get temperature() {
  return 5/9 * (this.pharma - 32);
  }
  set celTemperature(celsius) {
    this.pharma = celsius * 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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3223.0 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

There is no temperature setter, you’ve made a one called celTemperature instead

1 Like