0

I have been using Recursive Functions for quite a while now and I am completely baffled by my current problem. Here is my code:

var setbackArray = new Array();
setbackArray = [5, 15, 20];
var positionArray = new Array();
positionArray = ["28.0", "28.0", "24.4", "24.4", "24.4", "28.0", "28.0", "28.0", "28.0", "28.0", "24.4", "28.0", "28.0", "28.0", "24.4", "24.4", "24.4", "24.4", "24.4", "24.4", "24.4", "24.4", "24.4", "24.4", "18.5", "18.5", "18.5", "18.5", "22.1", "22.1", "22.1", "22.1", "28.0", "28.0", "28.0", "28.0", "38.6", "38.6", "32.7", "32.7", "38.6", "32.7", "38.6", "32.7", "32.7", "38.6", "38.6", "38.6", "32.7", "32.7", "32.7", "38.6", "32.7", "38.6", "32.7", "38.6", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "43.2", "22.1", "22.1", "22.1", "22.1", "22.1", "22.1", "22.1", "22.1", "22.1", "22.1", "32.7", "32.7", "32.7", "32.7", "38.6", "38.6", "38.6", "38.6"]
var recursive = function (i, length) {
    console.log('i: ' + i + ', length: ' + length);
    if (i < length) {
        var seatposition = Number(positionArray[i]).toFixed(1);
        console.log(seatposition);
        if (seatposition < setbackArray[setbackArray.length - 1] + 20 && seatposition > setbackArray[0] - 20) {
            console.log('Doing Some Math.....');
        } else {
            console.log('Not Usable');
            recursive(++i, length);
        }
        console.log('Doing More Math.......');
        console.log('Doing Even More Math.......');
        console.log('Doing Last Bit Of Math.......');
        console.log('Display Stuff On Screen');
        recursive(++i, length);
    } else {
        console.log('done checking');
    }
}
recursive(0, positionArray.length);

In the actual code, both arrays are created dynamically, I just coded them here so that you have a realistic sample. Basically I am going through all of the numbers in the positionArray and seeing if the number is less than the highest number in the setbackArray plus 20 and greater than the smallest number in the setbackArray minus 20. If it is I do some math with it to be used later. If it isn't I want it to move on to the next number in the positionArray.

The problem I am running into is that once i < length is no longer true, it shows "done checking" and then resets i to a previous value and continues to run. It does this without end and crashes the whole page.

I know that the issue lies right here:

} else {
      console.log('Not Usable');
      recursive(++i, length);
}

If I remove the recursive callout, it runs normally but performs the additional math that I don't want to perform on that number.

Any ideas?

Working sample here

7
  • Try recursive(i+1, length); instead - you seem to need i again? Commented Jan 27, 2015 at 20:58
  • Not relevant to your question, but why would you initialize setbackArray to new Array(); when you're just going to do setbackArray = [5, 15, 20] on the next line? var setbackArray = [5, 15, 20]; is all you need. Commented Jan 27, 2015 at 20:59
  • BTW, why do you have recursive(++i, length); on two separate lines? I think one of them doesn't belong there. Commented Jan 27, 2015 at 21:02
  • If the validation against the setbackArray fails, I want it to move on to the next number. If is succeeds, I want it to do some more work and then move on to the next number. Hence the two separate calls to continue on to the next number. Commented Jan 27, 2015 at 21:05
  • I don't understand the point of this line either: var seatposition = Number(positionArray[i]).toFixed(1); That converts the numeric string at positionArray[i] to a number, then back to a string. Everything you have in positionArray should come out of that unchanged. I'm not really sure why they're strings in the first place, since you're just comparing them to numbers. Commented Jan 27, 2015 at 21:11

1 Answer 1

2

It appears that what you intended in the problematic else block you identified is actually:

    } else {
        console.log('Not Usable');
        return recursive(++i, length);
    }

which would short-circuit the rest of the function for a "not usable" value.

As it is now, you're recursively calling your function twice in that scenario, leading to a forked control-flow as the recursion continues down multiple paths.

Another way to refactor and fix that section would be:

    var seatposition = Number(positionArray[i]).toFixed(1);
    console.log(seatposition);
    if (seatposition < setbackArray[setbackArray.length - 1] + 20 && seatposition > setbackArray[0] - 20) {
        console.log('Doing Some Math.....');
        console.log('Doing More Math.......');
        console.log('Doing Even More Math.......');
        console.log('Doing Last Bit Of Math.......');
        console.log('Display Stuff On Screen');
    } else {
        console.log('Not Usable');
    }
    recursive(++i, length);
Sign up to request clarification or add additional context in comments.

3 Comments

That is what appears to be happening, but how would I fix it? I want it to skip the rest of the remaining steps and continue the recursive on the next number.
I've provided two suggestions. The one that requires the least rearranging of code is to add a return statement in that else block.
That is what I needed, the "return" Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.