Skip to content

Instantly share code, notes, and snippets.

@cosmicbuffalo
Created May 10, 2017 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosmicbuffalo/96f17984fe09b8a59f28d3418f7eb63b to your computer and use it in GitHub Desktop.
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
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