Skip to content

Instantly share code, notes, and snippets.

@lubien
Forked from anabastos/show-me.js
Last active March 14, 2017 04:30
Show Gist options
  • Save lubien/90537a739cdcdc7ff04f7ae1f0ccf1c5 to your computer and use it in GitHub Desktop.
Save lubien/90537a739cdcdc7ff04f7ae1f0ccf1c5 to your computer and use it in GitHub Desktop.
/**
* Show Me the Evens - Show me the Odds
* Diana is learning to count and she just learned the difference between odds and even numbers.
* She wants to have some fun, so she picks a random number.
* If that number is even, she decides to count all the even numbers up to it starting from 0 up to (but not including) the input.
* If not, she decides to count all the odd numbers up to that number starting from 1 (but not including) the input.
**/
function counting(x){
return [...Array(x).keys()]
.filter(even(x) ? even : odd);
}
function even(x){
return x % 2 === 0
}
function odd(x){
return x % 2 === 1
}
console.log(counting(10)); //[ 0, 2, 4, 6, 8 ]
console.log(counting(11)); //[ 1, 3, 5, 7, 9 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment