Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created August 30, 2011 22:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/1182244 to your computer and use it in GitHub Desktop.
Save juandopazo/1182244 to your computer and use it in GitHub Desktop.
JavaScript without logical operators or loops
function _if(truthy, then) {
[then, function () {}][+!truthy]();
}
function ifElse(truthy, then, _else) {
[then, _else][+!truthy]();
}
function and(a, b) {
return !!((!!a + !!b) >> 1);
}
function or(a, b) {
return !!(!!a + !!b);
}
function _for(from, cond, augment, fn) {
var i = from;
_if(cond(i), function step() {
fn(i);
i = augment(i);
_if(cond(i), step);
});
}
function _while(cond, fn) {
_if(cond(), function () {
fn();
_while(cond, fn);
});
}
@dciccale
Copy link

y ahora sin usar < == > ?? :P

@juandopazo
Copy link
Author

Me acordé de esto de casualidad. Corregí el or y el and.

and ahora no usa >=. (!!a + !!b) primero pasa a y b a boolean y la suma los pasa a numeros 0 o 1. La suma de eso puede valer 0, 1 o 2. En binario esto es 0, 1 o 10. El operador >> mueve el numero binario a la derecha poniendo un cero en su lugar. Es decir, 0 >> 1 == 0, 1 >> 1 == 0 y 10 >> 1 == 1. Eso lo vuelvo a pasar a boolean y voila!

@dciccale
Copy link

dciccale commented Mar 7, 2012

eaa! se podía jaja congratz

@b2whats
Copy link

b2whats commented Apr 6, 2016

it's great

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