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
recursive(i+1, length);instead - you seem to neediagain?setbackArraytonew Array();when you're just going to dosetbackArray = [5, 15, 20]on the next line?var setbackArray = [5, 15, 20];is all you need.recursive(++i, length);on two separate lines? I think one of them doesn't belong there.var seatposition = Number(positionArray[i]).toFixed(1);That converts the numeric string atpositionArray[i]to a number, then back to a string. Everything you have inpositionArrayshould 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.