Kindly solve this problem

PLEASE TELL ME WHERE I AM MAKING MISTAKES :

**Getter & Setter **


// Only change code below this line
class Thermostat{
constructor(Fahrenheit){
  this.Fahrenheit = Fahrenheit;
}
//getter
get temparature(){
  return (5 / 9) * (this.fahrenheit - 32);
} 
//setter
set temparature(Celius){
  this.fahrenheit = (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

Your browser information:

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

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

Link to the challenge:

What problems are you having?

  1. The constructor, get, and set should be in the class brackets.
  2. I made some changes to the constructor, shown below if you want that spoiled. You need to replace some of the code with the same changes i made here
  3. misspelled temperature

/> / Only change code below this line

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

Hi!

Your code seems to be OK !

BUT: temparature != temperature (a != e)

One more advice!

A setter is normally to set directly a value. You do not need any calculations. For example:

set temperature(fahrenheit) {
this.Fahrenheit = fahrenheit;
}

1 Like

Hey there! You have done a good job!

You just need a quick fix on the capitalization and spelling errors;

  1. Change all the “Fahrenheit” to “fahrenheit” (all small letters)
  2. Change the “Celius” in setter to “celsius” (it should be all small letters and it was wrongly spelt).

This should fix the problem. Cheers!!

2 Likes

thanks a lot Chandant

2 Likes