Last active
September 21, 2015 09:48
-
-
Save johanalkstal/df1464d122ae4c1b6bfa to your computer and use it in GitHub Desktop.
Get sum of numbers using destructuring
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 sum(numbers) { | |
return loop(0, numbers); | |
function loop(total, numbers) { | |
if (numbers.length === 0) { | |
return total; | |
} | |
let [head, ...tail] = numbers; | |
return loop(head + total, tail); | |
} | |
} | |
let total = sum([1,2,3,4,5]); | |
console.log(`The total is ${total}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment