Created
January 29, 2019 19:28
-
-
Save tarasowski/fc758f07cfb6f117caf27a2f9e21f57e to your computer and use it in GitHub Desktop.
Calculate sum of an array recursively
This file contains hidden or 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
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