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

### Like this:

Like Loading...

*Related*

Am i missing something?

For #1, i found this to be the shortest:

while(a=f.pop(),a.length);a()

at 29 chars. This is a variation of evilpie’s solution

http://javascriptweblog.wordpress.com/2010/08/17/a-js1k-byte-saver-quiz/#comment-482

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.

Thanks for updating and thank you for giving credit where credit is due. Just want to make sure you got the shortest one.

For #6, I think I have a shorter solution: !(n%1)

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.

@evilpies

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

A few observations:

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

These are the same length:

Math.max(a,b,c)!=b&&Math.min(a,b,c)!=b

with(Math)max(a,b,c)!=b&&min(a,b,c)!=b

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

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)

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.

sorry my bad – {} needs wrapping in parens for syntax to be correct (original comment updated)

({})%1

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.

Funny – I rechecked source – I have two minus signs there. I’ll try using char codes in a sec

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

a.push.apply(a,b);

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

n = ‘string’;

n+’.’==n;

false

n = [1,2,3];

n+’.’==n;

false

n = {1:1}

n+’.’==n;

false

n = 1.7

n+’.’==n;

false

n = 7;

n+’.’==n;

true

Hey Anony. Yeah couple of senior moments there – sorry. Your solution to 6 is very cool

what’s wrong with the following for #3?

`(a>b&&b>c)||(c>b&&b>a);`

Its fine but the question said no >, < or arithmetic ops

Ah! – the question on this answers post left out the bit about comparison operators ;)

whoops you’re right – wordpress ate them up. Fixed now I hope – thanks for pointing out

Great article learned many things on JavaScript.

Isn’t this valid for #4 as well?

It works as far as i can see

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

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?

Nice try ;-)

But to return a real boolean you would need an extra 4 chars:

!!(Math.max(a,b,c)!=b&Math.min(a,b,c)!=b)

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?

ok, you win. ;-)

:)

Great blog,

I have a shorter answer for 5 (swap a and b) :

[a,b]=[b,a] (11 chars vs 13)

thanks axl – unfortunately that only works in firefox right now

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

3) Math.max(a,c)>b&&Math.min(a,c)<b

I think using the modulo operator would work for #6:

!(n%1);

Revised answer to #3:

i=[a,b,c].sort()[1],i!=a&&i!=c;