Skip to content

Instantly share code, notes, and snippets.

@hulkish
Created October 13, 2016 13:59
Show Gist options
  • Save hulkish/0dedaff5572a0f41e6a361a64ff0084a to your computer and use it in GitHub Desktop.
Save hulkish/0dedaff5572a0f41e6a361a64ff0084a to your computer and use it in GitHub Desktop.
let beforeInts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 112, 113];
let afterInts = evenify(beforeInts);
console.log('beforeInts: ' + beforeInts);
console.log('afterInts: ' + afterInts);
function evenify(ints, currentIndex = 0, evenInts = []) {// es6 feature for default argument values
let currentInt = ints[currentIndex];
if (currentInt % 2 === 0) {
evenInts.push(currentInt);
}
if (++currentIndex >= ints.length) {
return evenInts;
}
return evenify(ints, currentIndex, evenInts) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment