I can't understand why the following code snippet leads to error. Any ideas?
Maximum call stack size exceeded
function reverseArrayInPlace(array, low, high) {
if (low == undefined) {
low = 0;
}
if (high == undefined) {
high = array.length - 1;
}
if (low >= high) {
return;
}
var temp = array[low];
array[low] = array[high];
array[high] = temp;
return reverseArrayInPlace(array, low++, high--);
}
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
if (low >= high)isn't being hit. You always fall through to the recursive case, which the JS engine eventually blocks because otherwise it would go on forever. Maybe that will help you debug the algorithm.console.logcalls to track what values are being passed in.