13
\$\begingroup\$

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?

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

2 Answers 2

7
\$\begingroup\$

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))
\$\endgroup\$
4
  • 3
    \$\begingroup\$ 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... \$\endgroup\$ Jan 19, 2018 at 0:19
  • \$\begingroup\$ @ETHproductions <insert here> -> 0),(true :) \$\endgroup\$
    – ngn
    Jan 19, 2018 at 0:20
  • \$\begingroup\$ 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... \$\endgroup\$ Jan 19, 2018 at 1:52
  • \$\begingroup\$ Your newer solution is amazing, +1 from me! \$\endgroup\$ Jan 21, 2018 at 23:53
4
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – tsh
    Jan 18, 2018 at 2:11
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Arnauld
    Jan 18, 2018 at 2:32
  • \$\begingroup\$ I got a high score of <s>15</s>16 iterations!!! \$\endgroup\$ Jan 18, 2018 at 18:16
  • \$\begingroup\$ @MagicOctopusUrn Come on, you can do better than that! Insert coin and play again! \$\endgroup\$
    – Arnauld
    Jan 18, 2018 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.