我用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);
}
而且我不知道为什么结果arraySum([[1,2,3],4,5])
是 6。为什么不处理第一个数组之后的元素?