Skip to content

Instantly share code, notes, and snippets.

@kostandy
Last active April 26, 2019 09:48
Show Gist options
  • Save kostandy/d4e3f559e15999b3a760092ef7240d00 to your computer and use it in GitHub Desktop.
Save kostandy/d4e3f559e15999b3a760092ef7240d00 to your computer and use it in GitHub Desktop.
Recursive sum of array of numbers
const sum = arr => {
return arr.length ? arr[arr.length - 1] + sum(arr.splice(1)) : 0;
}
const arr = [1, 2, 3, 4, 5, 6, 7];
sum(arr); // 49
export default sum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment