Created
July 15, 2015 12:02
-
-
Save oozzal/1a4cd105f4dccb8f1851 to your computer and use it in GitHub Desktop.
Recursive Array Sum in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arraySum(arr) { | |
if(typeof arr == 'number') return arr; | |
return arr.filter(function(item) { | |
return Array.isArray(item) || Number.isFinite(item); | |
}).reduce(function(memo, item) { | |
return memo + arraySum(item); | |
}, 0); | |
} | |
var arr = [7,4,1, [3,true,2], 1, '123']; | |
alert(arraySum(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment