Skip to content

Instantly share code, notes, and snippets.

@kylejeske
Last active September 20, 2019 12:18
Show Gist options
  • Save kylejeske/741310e01493309d34314ab9b43dad18 to your computer and use it in GitHub Desktop.
Save kylejeske/741310e01493309d34314ab9b43dad18 to your computer and use it in GitHub Desktop.
Double Numbers using Recursion in JS
/**
* double-numbers.js
*
* Translates an Array of Numbers to Double that using Recursion and some ES6 tricks.
* @some(techniques).included([expansion assignment, rest operator, spread operator, ternary operator, arrow functions])
*/
((numbers=[]) => {
const doubleNumbers = ( collection = [], [ head, ...tail ] = collection ) =>
(collection.length === 0)
? []
: [].concat([head * 2], doubleNumbers(tail))
console.log(`Start with ${numbers.toString()}, and End with ${(doubleNumbers(numbers)).toString()}`);
})([2,4,6,8]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment