Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active March 18, 2017 23:33
Show Gist options
  • Save Woodsphreaker/9ef6c0c2ba5f33f672613f8f1f116e67 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/9ef6c0c2ba5f33f672613f8f1f116e67 to your computer and use it in GitHub Desktop.
Show Me the Evens - Show me the Odds
const numbersGenerator = (numbers) => [].concat(...Array(numbers).keys());
const props = [
(num) => num % 2 === 0,
(num) => !props[0](num)
]
const result = (range, type) => numbersGenerator(range).filter(_a => props[type](_a))
console.log(result(20,0));
console.log(result(20,1));
// caso de uso
// result(20,0) => Gera um array com 20 posições e retorna todos os números pares (opção 0)
// result(20,1) => Gera um array com 20 posições e retorna todos os números ímpares (opção 1)
# 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 numbersGenerator = (numbers) => [].concat(...Array(numbers).keys());
const props = {
"even": (num) => (num % 2 === 0),
"odd": (num) => !props['even'](num)
}
const result = (range, type) => numbersGenerator(range).filter(props[type])
console.log(result(20,'even'));
console.log(result(20,'odd'));
@suissa
Copy link

suissa commented Mar 18, 2017

If that number is even, she decides to count all the even numbers...

If not, she decides to count all the odd numbers...

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