Created
December 26, 2014 08:03
-
-
Save crongro/6030c194c62f9909e521 to your computer and use it in GitHub Desktop.
recursive sum using Array reduce
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
var arr = [1,2,3,[4,5,[4,6,[4,5,[5,[5,[6,100,[21]]]]]]]]; | |
var result = (function getSum(arr) { | |
var sum = arr.reduce(function(pre,cur,i,o){ | |
if(typeof cur !== "number") { | |
return pre + getSum(cur); | |
} | |
return pre+cur; | |
}); | |
return sum; | |
})(arr); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment