ES6 - Use getters and setters to Control Access to an Object

Tell us what’s happening:
I’m not sure how to do math up against a property, I made some Pseudo-pseudo code to try and explain what I am attempting if someone could let me know if I’m going in the right direction

Your code so far

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this._temp = fahrenheit;
  get celcius() {
  return this._(5/9*(temp - 32));
}
  set temp (updatedTemp) {
  this._temp(5/9*(temp - 32)) = celcius;
}
  }
}
// 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

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

Link to the challenge:

This looks roughly correct. There’s couple details, getter and setter should be written specifically for the temperature property.

  • Constructor will be receiving temperature in Fahrenheit.
  • When getting temperature property it should return temperature in Celsius, similarly
  • When setting temperature property, the value set will be temperature in Celsius.

Thought I had it but the same errors are showing up in the console, should I be using specific keywords?

class Thermostat {
  constructor(temp) {
    this._temp = temp;}
  get temp () {
  return (5/9)*(this._temp - 32);
}
  set temp (celsius) {
    return this._temp = (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

Almost there. Take a look at the lines at the end, with example how the class is used.

Specifically:

let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;

There’s an attempt to access (get) temperature property and then to set it. However class from your post do not have the temperature property, nor the getter/setter for it. Your current version would work, if tests would expect the getter/setter for temp.

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