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

Tell us what’s happening:
I am using repl.it to see what the functions return. When I run the code in Repl.it, it returns “26”, which is right. However when I run the code in the freeCodeCamp console I get the following errors. I do not understand what I have done wrong.

// running tests
Thermostat should be able to be instantiated.
A getter should be defined.
A setter should  be defined.
// tests completed

Your code so far


/* Alter code below this line */
class Thermostat {
constructor(temperature) {
    this._temperature = temperature;
}
// getter
get fahrenheit() {
    return this._temperature;
}
// setter
set fahrenheit(toCelsius) {
    this._temperature = (5/9 * (temperature - 32));
}
}
/* Alter code above this line */

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; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0.

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

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

You have a parameter named toCelsius but you are not using it in your calculation.

Thank you @RandellDawson, I corrected my mistake and in addition I set the argument for the getter and setter to “temperature”. So the only remaining error message is "Thermostat should be able to be instantiated. I have tried googling, but I can’t seem to find a solution, thus I do not understand what I am doing wrong. Can you pinpoint me in the right direction?

My code is

class Thermostat {
    constructor(temperature) {
        this._temperature = temperature;
}
// getter
get temperature() {
    return this._temperature;
}
// setter
set temperature(newTemperature) {
    this._temperature = (5/9 * (newTemperature - 32));
}
}

If I initiate the class Thermostate by making the falling call:

const thermos = new Thermostat(32); 

What do you think the following should display to the console?

console.log(thermos.temperature);

Answer:

It should display 0, because 32 degrees Fahrenheit is 0 degrees Celsius.

Your getter method is not returning the value in Celsius. It is just returning the value you initiated it to. In this example call above, that value is 32.

Thank you for your help @RandellDawson. I see why I failed to pass the test. The content of the error message however is not clear. :slight_smile: But, onward I go! :slight_smile: