Skip to content

Instantly share code, notes, and snippets.

@johanalkstal
Last active September 21, 2015 09:48
Show Gist options
  • Save johanalkstal/df1464d122ae4c1b6bfa to your computer and use it in GitHub Desktop.
Save johanalkstal/df1464d122ae4c1b6bfa to your computer and use it in GitHub Desktop.
Get sum of numbers using destructuring
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