Byte-Saver Quiz: Answers

Here’s the answers to last week’s quiz inspired by the excellent JS1K contest (which you still have time to enter!). The goal was to provide the shortest possible solution to each problem.

My original solutions are in blue.
One of my answers turned out to be wrong so I colored it red.
Shorter solutions provided by respondents are shown in green (crediting the person who first provided it).

Thanks for the plethora of great answers. Hope this was fun (and maybe even useful!)

1. var f is an array of functions. One of them takes no formal parameters: Invoke it.
f.sort(function(a,b){return a.length-b.length})[0]();(53)
while(a=f.pop())a.length||a(); (30) //evilpie

2. Variable a is an array. Make an array of the non-falsey values in a. (ECMA 5)
a.filter(Boolean); (18)

3. Variables a, b and c reference numbers. Verify b is exclusively bound by a and c without using >, < or arithmetic operators (+, -, *, /)
b==[a,b,c].sort()[1]; (21) //fails for multi digit
Math.max(a,b,c)!=b&&Math.min(a,b,c)!=b; (38) //anony

4. Verify that x is true. Don’t use true, false or any other variable, value or function (apart from x)
x&&(x===!!x); (13)
x===!!x&&x; (11) //Chris S (duh!)

5. Swap the values of variables a and b without using another variable or property
a=[b,b=a][0]; (13) //(with thanks to @abozhilov)

6. Is n an integer?
~~n===n; (8)

7. x is an array of numbers. Get the maximum value in x
Math.max.apply(0,x); (20)

8. If b is not a member of array a, add it to the end of the array (ECMA 5)
~a.indexOf(b)||a.push(b); (25)

9. Make a string consisting of string x repeated n times
new Array(n+1).join(x); (23) (courtesy of Prototype.js)
Array(n+1).join(x); (19) //fearphage (duh!)
for(s=”;n--;)s+=x; (19) //kangax

10. Concatenate arrays a and b without using concat
a.push.apply(a,b); (18)

Some honorary mentions:
4. x==!!(x+’.’); //kangax
5. x^=y;y=x^y;x^=y; //anony – only works for integers
6. n+’.’==n; //anony
10. (a+”,”+b).split(); (17) //Dmitry A. Soshnikov – fails for nested arrays


33 thoughts on “Byte-Saver Quiz: Answers

    1. Hi fearphage – yep you’re right – I somehow missed evilpie’s very cool solution (2 hours of checking on Sunday afternoon clearly wasn’t enough ;-)). Your solution is also 30 (inc. semicolon) so evilpie gets the credit (sorry). But thanks for letting me know.

    2. Cool 🙂 Havin the solution for #1 besides some really famous people.
      I had this solution ´x===(x==x);´ for question #4, is there something wrong with it?
      How do you get avatars in the comments & do i really need to enter my name and such everytime?

      Congrats to everyone.

      1. Congrats to you too 🙂
        I think i set it up so you only need to add your name for guest comments
        As for avatar – whats wrong with your monster anway? 😉 I assume you need a wordpress login to change it

  1. A few observations:

    (a+”,”+b).split() fails for nested arrays.

    These are the same length:

    Both !(n%1) or n%1==0 are shorter than ~~n===n (which fails for numbers larger than 2147483648)

    (did you lose track of my answers?)

    1. Hi cowboy,
      1. Good catch on split issue with nested arrays
      2. The with version of the question solution is essentially the same as the non-with version. I just credited the first version of this solution I got.
      3. !(n%1) or n%1==0 fail when n is not a number (!([]%1) -> true, !(({})%1) -> true)

      1. For #3, I think your example with {} is just broken. For whatever reason, {} can’t be used everywhere a variable representing {} can be used, i.e. a = {}; a % 1 yields NaN, not a syntax error.

  2. Okay me again ><, i just went through answeres and found one odd thing, kangax solution for #9 doesnt work for me. I always got 'Syntax Error' for this one so i check it again, there is only one minus sign behind the n and not two.

  3. 10. we know a is an array, so this is shorter:

    6. You write: “6. n+’.’==n; //anony – assumes a number”, why does this assume a number?

    n = ‘string’;

    n = [1,2,3];

    n = {1:1}

    n = 1.7

    n = 7;

      1. Ah! – the question on this answers post left out the bit about comparison operators 😉

    1. Well I really like it – but sadly it fails the test.

      Question said “Verify that x is true. Don’t use true, false or any other variable, value or function”

      0 is a value

  4. Please educate me regarding this late comment 🙂

    Couldn’t the solution Math.max(a,b,c)!=b&&Math.min(a,b,c)!=b; be shortened by one character by using bitwise AND? Math.max(a,b,c)!=b&Math.min(a,b,c)!=b;

    It would (according to the MDC docs at least) produce 0 or 1, and that is booly enough, right?

      1. Yes, I know, but the question only states that we should “verify that …” and not that the expression should return a boolean.

        That was why I used the term booly (which I guess is not standard). Wouldn’t it be enough that the value of the expression is falsy or truthy?

      1. indeed, too bad:( know any place where i could find more problems like this (with answers) ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s