Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Created January 29, 2019 19:28
Show Gist options
  • Save tarasowski/fc758f07cfb6f117caf27a2f9e21f57e to your computer and use it in GitHub Desktop.
Save tarasowski/fc758f07cfb6f117caf27a2f9e21f57e to your computer and use it in GitHub Desktop.
Calculate sum of an array recursively
const arr = [1, 2, 3]
const sum = arr =>
arr.length === 0
? 0
: arr[0] + sum(Array.prototype.slice.call(arr, 1))
sum(arr) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment