Skip to content

Instantly share code, notes, and snippets.

@jackfranklin
Created February 7, 2013 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackfranklin/4731766 to your computer and use it in GitHub Desktop.
Save jackfranklin/4731766 to your computer and use it in GitHub Desktop.
/***
*
* I want to execute someFunc() if the variable x is greater than 5
* how would you rewrite the below to NOT use a conditional? (EG no "if" used)
*
* **/
if(x > 5) {
someFunc():
}
@markjs
Copy link

markjs commented Feb 7, 2013

x > 5 && someFunc();

@jamierumbelow
Copy link

var result = (x > 5) ? someFunc() : null;

@kitop
Copy link

kitop commented Feb 7, 2013

x > 5 && someFunc()

@JeffreyWay
Copy link

x > 5 && someFunc();

@latentflip
Copy link

How about without using any (explicit) boolean operators:

var id = function(){};

[someFunc,id,id,id,id].slice(-1*Math.max(x,1),1)[0]();

This ain't as complicated as it looks (not that you would ever do it). Breaking it down:

We need a function that if we call it, it won't raise an exception, nor really do anything. Operations which don't do anything are "identity" operations, so this is the identity function:

var id = function() {};

Let's put our desired function (someFunc) in an array with 4 identity functions, we'll see why in a second:

var theFunctions = [someFunc, id, id, id, id];

We can slice (get a part of) an array with .slice(index,n) where index is the start position to slice from, and n is the number of items to take.

If index is negative, it will slice from the end, so:

[1,2,3,4,5].slice(-1,1) => [5]
[1,2,3,4,5].slice(-5,1) => [1]

Interestingly, if the negative number is bigger than our array, it just starts at the start:

[1,2,3,4,5].slice(-100,1) => [1]

This means that for our array: [someFunc, id, id, id, id] if we .slice(n,1) where n is anything <= -5 we will get [someFunc].

If we have [someFunc] we can then call someFunc by getting the first element of our one element array and calling it: [someFunc][0]().

Putting it all together we now have:

theFunctions.slice(-1*x, 1)[0]()

Which will work great unless x is less than one. In which case it will start slicing from the beginning again, so we can ignore x < 1 using Math.max(x,1) which will return x if x > 1 otherwise it will return 1 which won't call our function anyway.

Phew! So putting that together we get:

var id = function(){};

var theFunctions = [someFunc, id, id, id, id];

var limitedX = Math.max(x,1);

var sliceStart = -1 * limitedX;

[someFunc,id,id,id,id].slice(sliceStart,1)[0]();

Which if you substitute in all the vars you get:

[someFunc,function(){},function(){},function(){},function(){}].slice(-1*Math.max(x,1),1)[0]();

😱 😄

@davetheflashguy
Copy link

I would have used jamie's approach (no need to rewrite it :) )

@danharper
Copy link

I'm slow to seeing the tweet, but I'll comment anyway. x > 5 && someFunc()

@latentflip
Copy link

Man, I could come up with these all day, much fun 😄

This one's pretty useless, though an interesting exercise in getting it to actually work. Relies on you not caring about the return value of someFunc.

var self = function() {
  return self;
}

var first = function(fn) {
  return function() {
    fn();
    return self;
  }
}

var wrap = function(fn) { 
  return function() { return fn }
}

var wrapped = wrap(wrap(wrap(wrap(first(someFunc)))));

var limit = Math.max(x,1);

while(limit--) { wrapped = wrapped(); }

@latentflip
Copy link

var foo = setTimeout(someFunc, 5); setTimeout(function() { clearTimeout(foo) }, x);

@willh
Copy link

willh commented Feb 8, 2013

HEY JACK WHAT ABOUT x > 5 && someFunc();

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