Skip to content

Instantly share code, notes, and snippets.

@oozzal
Created July 15, 2015 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oozzal/1a4cd105f4dccb8f1851 to your computer and use it in GitHub Desktop.
Save oozzal/1a4cd105f4dccb8f1851 to your computer and use it in GitHub Desktop.
Recursive Array Sum in Javascript
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