{{ message }}

Instantly share code, notes, and snippets.

# ronaiza-cardoso/show-me.js

Last active Mar 27, 2017
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. **/ 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) {

### suissa commented Mar 13, 2017

 Ta ai a versão funcional ```const showEvens = ( num ) => Array.from( { length: num }, ( k, v ) => { if ( !(v % 2) ) return v } ) .filter( num => num !== undefined ) const showOdds = ( num ) => Array.from( { length: num }, ( k, v ) => { if ( v % 2 ) return v } ) .filter( num => num !== undefined ) const magic = ( num ) => !( num % 2 ) ? showEvens( num ) : showOdds( num ) const myNumbahs11 = magic(11) const myNumbahs12 = magic(12) console.log(`myNumbahs11: `, myNumbahs11) console.log(`myNumbahs12: `, myNumbahs12)```

 Pensei em algo assim https://gist.github.com/anabastos/db389fbd9c3d830fd550ca21fb07271e

### vinicius73 commented Mar 13, 2017

 ```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

### lubien commented Mar 14, 2017

 Nice, @lubien. Ａｅｓｔｈｅｔｉｃｓ. 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)```

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

### beatorizu commented Mar 18, 2017

 ```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; }```

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