alankis

Untitled

Oct 8th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // declare function arraySum
  2. function arraySum(i) {
  3. // var curentVal will hold current value of i[j] member    
  4. // at first value of sum is ZERO
  5. var sum = 0;
  6.     // i will be an array, containing integers, strings and/or arrays like itself.
  7.     // Sum all the integers you find, anywhere in the nest of arrays.
  8.    
  9.     for(var j = 0; j < i.length; j++) {
  10.         // curentVal = i[j];
  11. // if curentVal of i[j] is Array        
  12.         if(i[j] instanceof Array) {
  13.             sum += arraySum(i[j]);
  14.         } else if (typeof i[j] === 'number') {
  15.             sum += i[j];
  16.         }
  17.     }
  18.     return sum;
  19. }
  20.  
  21. console.log(arraySum([[1,2,false],'4','5']));    // 3
  22. console.log(arraySum([[1,2,3],4,5]));            // 15
  23. console.log(arraySum([[[[[[[[[1]]]]]]]], 1]));    // 2
Advertisement
Add Comment
Please, Sign In to add comment