Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Last active December 14, 2017 19:36
Show Gist options
  • Save vinicius73/6259eb4feb1ac0438fcb6b3c392f0a62 to your computer and use it in GitHub Desktop.
Save vinicius73/6259eb4feb1ac0438fcb6b3c392f0a62 to your computer and use it in GitHub Desktop.
Um exercício simples com js

Default true

Existem 3 entradas possiveis

  • true
  • false
  • undefined

as saídas serão:

  • undefinded => true
  • true => true
  • false => false

Crie uma função que avalie essas condições em apenas 1 (uma) linha   O objetivo é gerar o menor código possivel.

Solução

value => !(value === false)

// uglify
x => !(x === !1)

Parabéns @fmsouza

@wilcorrea
Copy link

boouuuaaaa

Copy link

ghost commented Feb 17, 2017

Os cara é ninja nos Ogroritmo!

@IgorDePaula
Copy link

Vou tatuar esta funcao XD

@matheushrt
Copy link

const isFalse = x => x !== false

@theuves
Copy link

theuves commented Mar 17, 2017

@vinicius73 @fmsouza

fiz outra com três caracteres a menos.

x => !(x < 1)

alguns testes:

// definindo-a como função `f`.
const f = x => !(x < 1);

// temos:
f(true); // true
f(false); // false
f(undefined); // true

// onde todas as saídas são valores booleanos.
typeof f({}) === "boolean"; // true
typeof f([]) === "boolean"; // true
typeof f("") === "boolean"; // true
typeof f(/(?:)/) === "boolean"; // true

explicando a função com exemplos:

Number(true) // 1
Number(false) // 0
Number(undefined) // NaN

1 < 1 // false
0 < 1 // true
NaN < 1 // false

!false // true
!true // false
!false // true

@theuves
Copy link

theuves commented Mar 17, 2017

@vinicius73 @fmsouza

outra função que também é menor que a de vocês é:

(x = 1) => !!x

mas tem 1 caractere a mais do que a outra que eu postei.

(ela também passa em todos os testes que fiz.)

@vinicius73
Copy link
Author

Mandou muito bem @theuves
image

@jpcrs
Copy link

jpcrs commented Dec 14, 2017

x => x!=0

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