Created
May 10, 2017 04:23
-
-
Save cosmicbuffalo/96f17984fe09b8a59f28d3418f7eb63b to your computer and use it in GitHub Desktop.
Recursive function to get sum of items in an array, written 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 (arr.length < 1){ | |
return 0 | |
} else if (arr[0].constructor === Array){ | |
return arraySum(arr[0]) + arraySum(arr.slice(1)) | |
} else { | |
return arr[0] + arraySum(arr.slice(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment