I have written the following program in javascript:
function recursiveSum(a) {
sum = 0;
for (i=0;i<a.length; ++i) {
if (typeof a[i] === "number") {
sum += a[i];
} else if (a[i] instanceof Array) {
sum += recursiveSum(a[i]);
}
}
return sum;
}
function arraySum(a) {
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
return recursiveSum(a);
}
And I can't figure out why the result of arraySum([[1,2,3],4,5]) is 6. Why the elements after the first array are not processed?
sumvar defined? It may not be local to the function.var.varin front of the variables and so my variables are global as the answers suggested.