Object fails to Instantiate

In the “Thermostat” exercise, even with FCC’s suggested code, I’m getting an error message “Thermostat should be able to be instantiated”. I can’t find what’s causing the problem.

The code (so far):

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this._fahrenheit = fahrenheit;
  }

  //getter
  get temperature() {
    return (5/9) * (this.fahrenheit) - 32;
  }

  //setter
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9)/5 + 32;
  }
}
// Only change code above this line
const thermos = new Thermostat(76); // Setting in Fahrenheit scale 

All advice appreciated, thanks

PeterL

Your browser information:

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

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

Link to the challenge:

In constructor you are setting this._fahrenheit variable, but later that variable is not used anywhere.

1 Like

Hi Sanity,

Yes, I spotted that one and reset the code to this.fahrenheit = fahrenheit, then re-ran the tests, all happy and looking forward…Same result, no joy.

Thanks for your suggestion, I appreciate your time

PeterL

Can you please post the updated code.

Hi Jeremy,

Sorry I’m late with the reply (I’m a taxi-driver. Although the demand has collapsed, there are still times when I get a few jobs consecutively, and therefore can be away for a while)

The code as it stands now is

// Only change code below this line
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }

  //getter
  get temperature() {
    return (5/9) * (this.fahrenheit) - 32;
  }

  //setter
  set temperature(celsius) {
    this.fahrenheit = (celsius * 9)/5 + 32;
  }
}
// Only change code above this line
const thermos = new Thermostat(76); // Setting in Fahrenheit scale 

Many thanks,

PeterL

Hmm, I am able to instantiate the class, so I am not sure why the test suite believes that you cannot.

As a note not related to your issue, it is typical to preface internal class variables with an underscore like _fahrenheit.

Right now it might be more just an indicator that your class is returning wrong result, rather than instantiation issues. Take a look at it, add console.log(thermos.temperature) at the bottom of your code, for Thermostat(76) result should be 24.44.

Thanks guys,

I’m wrapping it up for this evening. Will resume hostilities tomorrow afternoon, and give your suggestions a try.

Many thanks,

PeterL

1 Like

Hi Sanity,

I tried your suggestion, putting the suggested code in at the bottom of my section of code. No joy, I’m afraid. It just brought out a slew of additional error-messages.

Well and truly flummoxed! I’m getting on with the rest of the course, and will return to this from time to time to see if, as I progress with JS, I spot something new.

Many thanks, PeterL

It wasn’t supposed to fix the problem, but to show you what is wrong…

Aah! Maybe I’m being slow. I’ll get back to it later on…

Hi PeterL,

Seems to be a simple bracket notation error.

Take another look at your getter function.

I amended the return statement by moving a closing bracket and your code ran perfectly:

return (5/9) * (this.fahrenheit - 32);

The Free Code Camp environment forces you to place a bracket here, having it at the end gives you the correct calculation.

Hope this helps.

Hi Laurels,

Man, you’re officially my New Hero Of The Day! You’re right, it worked perfectly.

(I’m going to drag myself outside and give myself a good kicking for being so relentlessly dozy/blind-as-a-bat!)

I was a bit overwhelmed by the ES6 section, found it much harder going than previous lessons in FCC. Having several years’ experience as a teacher, I thought that, in this section, too many new concepts were rushed in within too short a time.

I was reduced at times to using FCC’s suggested solutions not as a check against which to measure my own work, but as a guide to help me understand what I had (supposedly) already learned. I’ll be revisiting the whole section later, when I’ve got a little more overall fluency with the concepts and logic of JS.

Nil desperandum. One of these days I’ll be a coder.

My heartfelt thanks , PeterL