How to solve the total task on "return true to win" in 21 chars

There is a site called "Return True to Win" with interesting tasks for Javascript programmers. The goal is to find arguments to a given function that force it to return true.

The following is one of the tasks:

function total(x) {
return (x < x) && (x == x) && (x > x);
}


The users must find snippets for the value of x that cause the function to return true. To test snippets, you call the function with your snippet as the parameter (i.e. total(<snippet>)).

I found a 22-character solution:

{valueOf:_=>n++%3},n=0


Some people found the solution in 21 chars. I can't find out this solution. What is the solution in 21 chars?

• You should link the contest/page the task is taken from. Jan 17, 2018 at 20:05
• alf.nu/ReturnTrue
– Oleg
Jan 17, 2018 at 20:08
• {valueOf:Math.random} works from time to time ... but I suspect that would infringe some rule... (or maybe not?) Jan 17, 2018 at 20:40
• Those are decidedly tricky; out of the first 24, I can only answer 14...
– Neil
Jan 17, 2018 at 22:03
• Salty dev note... having a function return Boolean called “total” is like nails on a chalkboard! Jan 17, 2018 at 22:28

21 chars

{valueOf:n=_=>n=2<<n}


My original joke, which got downvoted and proposed for deletion:

11 chars :)

total=_=>!0


Test:

function total(x) {
return (x < x) && (x == x) && (x > x);
}
var arg = total=_=>!0
console.log(total(arg))

• Good job thinking outside the box, but the challenge is to finish function total(x) { ... } total(<insert here>) and have the result of the total() call be true, so I don't think this will work... Jan 19, 2018 at 0:19
• @ETHproductions <insert here> -> 0),(true :)
– ngn
Jan 19, 2018 at 0:20
• Tried that, and also 0)||(true. Neither work though, and now I'm totally confused because there's another level where you have to do 0);(!0 or smth similar... Jan 19, 2018 at 1:52
• Your newer solution is amazing, +1 from me! Jan 21, 2018 at 23:53

Cheaty answer

I've already mentioned it in the comments, but it was not tested. It is now. You'll have to keep submitting it until it works.

{valueOf:Math.random}


Demo

function total(x) {
return (x < x) && (x == x) && (x > x);
}

for(i = 1; !total({valueOf:Math.random}); i++);

console.log('Returned true after ' + i + ' iteration(s)')

• Could you explain more about how this works? I just cannot understand how the x == x became true if x is chosen randomly every time.
– tsh
Jan 18, 2018 at 2:11
• @tsh The trick is that valueOf is not invoked when the equality test is processed. On the other hand, comparison operators expect primitive values, which forces valueOf to be called. Here is a test showing that. Jan 18, 2018 at 2:32
• I got a high score of <s>15</s>16 iterations!!! Jan 18, 2018 at 18:16
• @MagicOctopusUrn Come on, you can do better than that! Insert coin and play again! Jan 18, 2018 at 18:21