Profile image of tagNinja90395
Submitted by tagNinja90395
over 10 years

RangeError: Maximum call stack size exceeded?

What the heck does that mean

Answer 556c01f0e0a3003aec00035d

2 votes

Permalink

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
    }
};
Profile image of mtf
Submitted by mtf
over 10 years

2 comments

Profile image of tagNinja90395
Submitted by tagNinja90395
over 10 years

thnx

Profile image of rajeshjoshi
Submitted by rajeshjoshi
over 10 years

1

Answer 556bf14fe0a3007ff90004c8

1 vote

Permalink

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

Profile image of mtf
Submitted by mtf
over 10 years

Answer 556bf27de0a3002476000385

0 votes

Permalink

Here are some examples of recursive functions that work as expected, and don’t exceed the call stack size:

Profile image of mtf
Submitted by mtf
over 10 years

1 comments

Profile image of mtf
Submitted by mtf
over 10 years

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

0 votes

Permalink

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'
Profile image of mtf
Submitted by mtf
over 10 years

3 comments

Profile image of mtf
Submitted by mtf
over 10 years

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.

Profile image of mtf
Submitted by mtf
over 10 years

Once we know something works, we can scale back on the debugging cues.

Profile image of tagNinja90395
Submitted by tagNinja90395
over 10 years

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

0 votes

Permalink

var isEven = function(n){
    return n.toString(10)+((!(n%2)||0)?' Even':' Odd');
};
console.log(isEven(255));
// output
255 Odd
Profile image of mtf
Submitted by mtf
over 10 years

Answer 556c0b61e0a3007ff90004d6

0 votes

Permalink

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.

Profile image of mtf
Submitted by mtf
over 10 years

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
Explore full catalog