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

13

2

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?

Oleg

Posted 2018-01-17T19:59:22.170

Reputation: 141

3You should link the contest/page the task is taken from. – Emigna – 2018-01-17T20:05:37.637

1https://alf.nu/ReturnTrue – Oleg – 2018-01-17T20:08:29.293

You can't see the total task without solving previous tasks, You can find solutions on https://gist.github.com/OlegTar/cc79df913cafb3045474578e8ada8a3d

– Oleg – 2018-01-17T20:08:53.580

@Riker Which JS engine are you using? Here is a working link.

– Arnauld – 2018-01-17T20:24:29.447

Oh, I misunderstood how to run this. I thougth the code was supposed to replace the above code. – Rɪᴋᴇʀ – 2018-01-17T20:25:13.357

2{valueOf:Math.random} works from time to time ... but I suspect that would infringe some rule... (or maybe not?) – Arnauld – 2018-01-17T20:40:57.237

1Those are decidedly tricky; out of the first 24, I can only answer 14... – Neil – 2018-01-17T22:03:05.127

4Salty dev note... having a function return Boolean called “total” is like nails on a chalkboard! – scunliffe – 2018-01-17T22:28:34.863

@scunliffe It's probably an adjective like in the algebraic structure total order. Total here means any two elements of a set are comparable. It's not the bottom line of a restaurant bill. Just my guess.

– ngn – 2018-01-21T04:05:25.603

Answers

7

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))

ngn

Posted 2018-01-17T19:59:22.170

Reputation: 11 449

3Good 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... – ETHproductions – 2018-01-19T00:19:57.793

@ETHproductions <insert here> -> 0),(true :) – ngn – 2018-01-19T00:20:59.683

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... – ETHproductions – 2018-01-19T01:52:01.763

Your newer solution is amazing, +1 from me! – ETHproductions – 2018-01-21T23:53:48.553

4

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)')

Arnauld

Posted 2018-01-17T19:59:22.170

Reputation: 111 334

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 – 2018-01-18T02:11:26.483

1

@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.

– Arnauld – 2018-01-18T02:32:58.450

I got a high score of <s>15</s>16 iterations!!! – Magic Octopus Urn – 2018-01-18T18:16:44.897

@MagicOctopusUrn Come on, you can do better than that! Insert coin and play again! – Arnauld – 2018-01-18T18:21:57.570