ES6 Object Instantiation

The below code throws the error that a Thermostat can’t be instantiated, but based on the code below, it clearly can be because the console.log(thermos); returns an object and console.log(thermos instanceof Thermostat); proves that it is an instance of the object that is being tested for.

/* Alter code below this line */
class Thermostat {
    constructor(temperature) {
        this._temperature = temperature;
    get temperature() {
        return this._temperature;
    set temperature(updatedTemp) {
        this._temperature = updatedTemp;
/* 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
console.log(thermos instanceof Thermostat);

I have also tried a different browser. I’m really not sure what is wrong here. Can anyone confirm that this code works or doesn’t work?

I should be able to initiate a new Thermostat instance (like the code below does):

const thermos = new Thermostat(76);

and if I reference the temperature property of it, (like below) I see get 24.44.

console.log(thermos.temperature); // should display 24.44

The temperature passed to the initial call to new Thermostat is assumed to be in Fahrenheit. The temperate returned from referencing the temperature property of an instance should be in Celsius. Also, the instructions tell you that you should be able to set the temperature of a Thermostat instance via the temperature property like below:

const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
console.log(thermos.temperature); // should display 26

Your function makes no conversions at all. You need to think about your solution a bit more.

Thanks Randell, that was indeed the reason for "Thermostat should be able to be instantiated."

When I read through the challenge it wasn’t really clear with the following statements being contradictory:

Now create a getter and a setter in the class, to obtain the temperature in Celsius.

Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.

This is the power of a getter and a setter. You are creating an API for another user, who can get the correct result regardless of which one you track.

I’m guessing that this contradiction and some real life distractions caused me to be stumped because the only test my code wasn’t passing was due to the object apparently not being able to be instantiated, so when I looked for errors it was to do with it not being able to be instantiated. But actually, it was due to me not choosing a temperature scale.

assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());

I think that the free code camp curriculum challenge could either test using instaceof stead of a specific value (because that’s what the test says it’s going to test for) or the challenge description should be clearer that the user should implement a Celsius scale conversion.

Hope this helps.

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