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
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
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.
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.
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.