RangeError: Maximum call stack size exceeded?
What the heck does that mean
Answer 556c01f0e0a3003aec00035d
Ah, I got ahead of myself and put a whole lot on your plate. No apology, though. It’s good. Treat it like caviar. You don’t taste it much, but man it sure piques the senses! (Not to be vain, I’m only being droll.)
var isEven = function(number){
if (number % 2){
return false; // not true
} else {
return true; // not false
}
};
Answer 556bf14fe0a3007ff90004c8
It means you have accidentally stumbled upon a recursive function that has no base case by which to exit. The program will be returning a call to itself repeatedly and each return value will be held in the call stack until the base case is reached, at which point it stops the recursion and reverses itself through the call stack.
Didn’t make a bit of sense, did it? That’s okay. Let us have a look at your code so we can see what ‘actually’ happened, the above possibility notwithstanding. We only really exceed call stack size when we’re running a recursive function that has gone off the rails, which is why I jumped at that explanation. I’ve been wrong before.
Again, please let us see your code (assuming you saved it first).
Answer 556bf27de0a3002476000385
Here are some examples of recursive functions that work as expected, and don’t exceed the call stack size:
1 comments
Don’t worry if none of this makes any sense. You’ll learn about eventually. For now, just be careful you don’t do it accidentally, again.
Answer 556c0054e0a3003aec00035c
Whoa!
var isEven = function(number) {
// Your code goes here
isEven(isEven % 2);
if (isEven(0)) { // ...
You see what I mean by a function calling itself? There are two calls here, and they are both inside the function. Sure glad you did save this code, cause we never would have been able to dream this up…
No offense. Stuff like this happens. Hopefully we learn from it. Shake it off. Now let’s solve this so the problem goes away.
var isEven = function(number){
return [number,!(number%2) || false];
};
console.log(isEven(Math.floor(Math.random()*2)));
Nice call, btw on the binary outcome. Plays directly into this demo. The outcomes are one of,
[ 1, false ] // odd
// or
[ 0, true ] // even
Now we can enter any integer and get an expected response.
> isEven(255)
=> [ 255, false ]
> isEven(255)[1] ? 'even':'odd'
=> 'odd'
3 comments
Knowing that the logic works we can reign it down to just the logical expression, !(number%2)||false, which would make an isEven()?even:odd call less cryptic.
Once we know something works, we can scale back on the debugging cues.
Thanks but could you explain the second slide (the one with the code you wrote) i wanna know whats u with the return, and also what the heck does my picture represent
Answer 556c0537e0a300f2cf0003f7
Answer 556c0b61e0a3007ff90004d6
What if we write,
var isEven = function(n){
return n.toString() + ((0 || n % 2) ? ' Odd' : ' Even');
};
We get rid of the Linter caution about a confusing use of NOT and we streamline the expression.
console.log(isEven(255));
now gives,
255 Odd
as expected, with less overhead.
Popular free courses
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.4 Lessons
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.11 Lessons
- Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
- Beginner Friendly.6 Lessons