-
-
Save anabastos/db389fbd9c3d830fd550ca21fb07271e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ótima solução, só que da pra se repetir menos assim:

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