Skip to content

Instantly share code, notes, and snippets.

@anabastos
Forked from ronaiza-cardoso/show-me.js
Last active April 17, 2017 05:37
Show Gist options
  • Save anabastos/db389fbd9c3d830fd550ca21fb07271e to your computer and use it in GitHub Desktop.
Save anabastos/db389fbd9c3d830fd550ca21fb07271e 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.
**/
const counting = (x) => {
return arrayFrom(x)
.filter(even(x) ? even : odd)
}
const even = (x) => {
return x % 2 == 0
}
const odd = (x) => {
return x % 2 == 1
}
const arrayFrom = n => {
return Array.from({ length: n }, (el, index) => index)
}
console.log(counting(10)); //[ 0, 2, 4, 6, 8 ]
console.log(counting(11)); //[ 1, 3, 5, 7, 9 ]
@lubien
Copy link

lubien commented Mar 14, 2017

Ótima solução, só que da pra se repetir menos assim:
image

E seria interessante usar === (só pra garantir).

image

@anabastos
Copy link
Author

@lubien Valeu!! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment