I'm currently working on a javascript exercise from http://toys.usvsth3m.com/javascript-under-pressure/ and my code is not working on nested arrays...I'm trying to use recursion to solve the problem, but it only seems to be adding the first element in a nested array situation...I come from a Ruby background, so javascript is a little unfamiliar to me.
If anyone could point out what I'm doing wrong, I'd appreciate it!
Thanks, Smitty
function arraySum(i) {
// i will be an array, containing integers and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
var sum = 0;
sum = sumit(i);
return sum;
}
function sumit(i) {
var sum = 0;
for (a=0; a<i.length; a++)
{
if (typeof(i[a]) == 'array')
{
sumit(i[a]);
}
else
{
sum += parseInt(i[a]);
}
}
return sum;
}