Skip to content

Instantly share code, notes, and snippets.

@ronaiza-cardoso
Last active March 27, 2017 18:18
Show Gist options
  • Save ronaiza-cardoso/720cda408bbc899fc6c69bfddfbe6505 to your computer and use it in GitHub Desktop.
Save ronaiza-cardoso/720cda408bbc899fc6c69bfddfbe6505 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 count(x) {
var numbers = [];
if (x % 2 === 0) { // check that x is even
for (var i = 0; i < x; i += 2) { // assign i to 0; give i the limit of x; and increment by 2
numbers.push(i);
}
} else {
for (var j = 1; j < x; j += 2) { // assign i to 1; give i the limit of x; and increment by 2
numbers.push(j);
}
}
return numbers;
}
/* ES6 */
const count = x => {
if (x % 2 === 0) {
@anabastos
Copy link

anabastos commented Mar 13, 2017

@vinicius73
Copy link

const notUndefined = num => num !== undefined
const isOdd = num => ((num % 2) === 0)
const isEven = num => !isOdd(num)
const numberTest = (cp, num) => (cp(num) ? num : undefined)
const makeTest = cp => (k, v) => numberTest(cp, v)

const makeArrBuilder = cp => num =>
  Array.from({ length: num }, makeTest(cp)).filter(notUndefined)

const showEvens = makeArrBuilder(isEven)
const showOdds = makeArrBuilder(isOdd)

const magic = (num) => (isEven(num) ? showEvens(num) : showOdds(num))

const myNumbahs11 = magic(11)
const myNumbahs12 = magic(12)
console.log(`myNumbahs11: `, myNumbahs11)
console.log(`myNumbahs12: `, myNumbahs12)

uma evolução da versão do @suissa

@ryukinix
Copy link

ryukinix commented Mar 14, 2017

Nice, @lubien. Aesthetics. Por um pouco nem parece JavaScript.

Em Common Lisp:

(defun counting (n)
  (loop with f = (if (evenp n) #'evenp #'oddp)
        for x from 0 to below
        when (funcall f x)
          collect x))
(counting 10) ;; (0 2 4 6 8)
(counting 11) ;; (1 3 5 7 9)

@theuves
Copy link

theuves commented Mar 16, 2017

const count = x => [...Array(x).keys()].filter(y => !((x + y) % 2));

@beatorizu
Copy link

function show_odd_even(number) {
  var numbers = [];
  for (var cont = verify_odd_even(number); cont < number; cont+=2) {
    numbers.push(cont);
  }
  return numbers;
}

function verify_odd_even(number) {
  if (number % 2 === 0) {
    return 0;
  }
  return 1;
}

@ihavenonickname
Copy link

ihavenonickname commented Mar 21, 2017

who needs readability anyway

const show = n => [...Array(Math.ceil(n / 2) - n % 2).keys()].map(x => x * 2 + n % 2);

also, OP's comment in line 16 is wrong.

@andersonFaro9
Copy link

andersonFaro9 commented Mar 27, 2017

uhuhuhuuhhuhuhuhuhuhuhuhuhhuhuh funcional com Kotlin
val numbersPairsOdds = arrayOf(1, 3, 5, 6)
fun calc(x: Array<Int> = numbersPairsOdds) = x.map { if (it % 2 == 0) { print("número par -> $x , ") } else { print("número impar -> $x ") } }

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