RangeError: Maximum call stack size exceeded

Tell us what’s happening:
i got this error RangeError: Maximum call stack size exceeded when i set celsius(celsius){}; why does both celsius overlap?

and this is the entire error

set celsius(celsius){
               ^

RangeError: Maximum call stack size exceeded
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:8:16)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)
    at Thermostat.set celsius [as celsius] (/home/omar/Desktop/test.js:9:22)

Your code so far


// Only change code below this line
class Thermostat{
constructor(fahrenheit){
    this.fahrenheit = fahrenheit;
    this.celsius = 5/9 * (fahrenheit - 32);
    let temperature = this.celsius;
}
set celsius(celsius){
    this.celsius = celsius;
}

get celsius(){
    return this.celsius;
}
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
console.log("temp: " + temp);
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

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

Link to the challenge:

Your getter, setter, and parameter are all named celsius. This is creating conflicts in the value, which is leading to a recursive function call (essentially, it’s trying to set this.celsius as the celsius() function).

1 Like