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

@vinicius73
Copy link
Author

vinicius73 commented Feb 17, 2017

muito grande @cesarcosta99

@wilcorrea
Copy link

const is = (value) => value === undefined ? true : value;

@fmsouza
Copy link

fmsouza commented Feb 17, 2017

@vinicius73 ué, eu rodo aqui no inspect do Chrome e retorna certinho...

@vinicius73
Copy link
Author

vinicius73 commented Feb 17, 2017

não cumpre o requisito @wilcorrea

@vinicius73
Copy link
Author

vinicius73 commented Feb 17, 2017

@fmsouza @WendellAdriel @wilcorrea
Suas abordagens podem não retornar boolean

@WendellAdriel
Copy link

Por que não @vinicius73?
Fiz bem rápido aqui, testei no fiddle apenas mas achei que tava de boa....
https://jsfiddle.net/emnrnkpb/

@wilcorrea
Copy link

const is = (value) => value === undefined ? true : !!value;

@vinicius73
Copy link
Author

@wilcorrea ta muito perto

@vinicius73
Copy link
Author

vinicius73 commented Feb 17, 2017

se for um objeto por exemplo @WendellAdriel, pode não retornar boolean
falei errado quando disse que não cumpre, só não é self guard

@fmsouza
Copy link

fmsouza commented Feb 17, 2017

const checkBool = value => !(value === false);

Copy link

ghost commented Feb 17, 2017

const is = value => value === undefined ? true : !!value

Copy link

ghost commented Feb 17, 2017

hehe só tirei o ;

Copy link

ghost commented Feb 17, 2017

Não pode retornar bool?

@wilcorrea
Copy link

o @juscilan tirou os parênteses da minha e mandou : )

@vinicius73
Copy link
Author

só pode retornar boolean @juscilan

@fmsouza
Copy link

fmsouza commented Feb 17, 2017

@vinicius73 Sobre sua última explicação, acho que o que ta gerando confusão então é que lá no enunciado diz que as 3 entradas possíveis são undefined, true ou false.

@vinicius73
Copy link
Author

@fmsouza matou!!!

@fmsouza
Copy link

fmsouza commented Feb 17, 2017

💃 💃 💃 💃 💃 💃

@vinicius73
Copy link
Author

sim @fmsouza, o objetivo é ter a menor função, além da de vcs ainda ser "grande" pode não retornar boolean, mas sua ultima solução é a correta.

@wilcorrea
Copy link

Tendi não @vinicius73 e @fmsouza : (

@IgorDePaula
Copy link

ele comparou a entrada com false, se nao for false, retorna false e ele converte pra true, se a entrada for false, retorna true e ele converte pra false...realmente bem simples

@wilcorrea
Copy link

undefined === false = false ; )

@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