0

I understand what maximum call stack exceeded is. However, is there a workaround for this for my code? Furthermore, there will be a time where it eventually will stop looping, that is when position > counter1.

var a = 0;
var b = 1;
var c;
var counter1 = 1;
var position = 0;

window.onload = function() {
    var position = prompt("Please enter the position number.","0");
    calc1();
}

function calc1() {
    if(position <= counter1) {
    c = a+b;
        counter1++;
        calc2();
    }
    else {
    callResult();
    }
}

function calc2() {
    if(position <= counter1) {
    a = b+c;
    counter1++;
    calc3();
    }
    else {
    callResult();
    }
}

function calc3() {
    if(position <= counter1) {
        b = c+a;
    counter1++;
    calc1();
    }
    else {
    callResult();
    }
}

function callResult() {
    if (position %3 == 1) {
    document.getElementById("answer").innerHTML = a;
    }
    else if (position %3 == 2) {
    document.getElementById("answer").innerHTML = b;
   }
    else {
    document.getElementById("answer").innerHTML = c;
    }
}
Tan WS
  • 181
  • 1
  • 2
  • 12
  • 4
    How do you *ever* figure out the code flow if this is how you indent your code? – h2ooooooo Feb 10 '14 at 12:30
  • 1
    This is not how I normally indent. Its usually 4 spaces, then the next indent is a tab. But for this website you need to have 4 spaces to show it's code, so I tried to minimized time wasted so I can get answers quickly. But if it does affect people, I'll change it now – Tan WS Feb 10 '14 at 12:31
  • 2
    So, you increment counter1 when position is <= counter1, but never change position, so the counter gets bigger and bigger and thereby keeps calling calc1 -> calc2 -> calc3 -> calc1? etc.... – doctorlove Feb 10 '14 at 12:31
  • It stops when position > counter1. – Tan WS Feb 10 '14 at 12:33
  • and if it is always less, e.g. 0? – smnbbrv Feb 10 '14 at 12:34
  • It actually never stops in this code as @doctorlove pointed out – axelduch Feb 10 '14 at 12:35
  • @Simon I have not added that yet as there already is a problem now. But when this is solved I will add that. Thanks for the reminder. – Tan WS Feb 10 '14 at 12:36

3 Answers3

4

You should avoid recursion and use loop. Something like this:

window.onload = function() {
    var position = prompt("Please enter the position number.","0");
    maincalc();
}

function maincalc() {
    var subcalc = [ calc1, calc2, calc3 ];
    var calccount = 0;

    while(position <= counter1) {
        subcalc[ calccounter ]();
        calccounter = (calccounter +1) % 3;
    }
}
YK1
  • 7,327
  • 1
  • 21
  • 28
1

The value of position is given once and never changed.

You then check, if(position <= counter1) in each call calc1, calc2, calc3 which call each other thus:

calc1 -> calc2 -> calc3 -> calc1 -> ...

This will clearly continue until you run out of stack space.

Perhaps if you increment position instead of counter1 or keep calling while position is greater than the counter this problem will go away, i.e.

if(position > counter1)

You probably need to step back and think about what you are really trying to do.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • Thanks for pointing out my careless mistake. I actually wanted to increment counter1 and execute the code until counter1 is more than the position. – Tan WS Feb 10 '14 at 12:42
0

as I understand you are calculating Fibonacci numbers sum?

see this Javascript Fibonacci answer to learn how to do it much easier and without any recursive calls

Community
  • 1
  • 1
smnbbrv
  • 23,502
  • 9
  • 78
  • 109