JavaScript Quiz: What does this function do?

JavaScript Quiz: What does this function do?

Are you ready for a quick programming challenge? You will be presented with 18 short JavaScript functions. Your mission is to decipher what they do, and choose the correct option from the list. Good luck!

Tip: The code samples are available as a github gist for easier copying and pasting in your browser’s console.

What does this function do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Great job!

Now make your friends jealous:

Answer

12 of 22

Presenting Bootstrap Studio

a revolutionary tool that developers and designers use to create
beautiful interfaces using the Bootstrap Framework.

Learn more
Web Browser Frame DevKit Box Mouse Cursor
by Martin Angelov

Martin is a web developer with an eye for design from Bulgaria. He founded Tutorialzine in 2009 and it still is his favorite side project.

1Share this post
2Read one more article
3Get your free book
Book Cover
jQuery Trickshots

Tutorialzine's advanced jQuery techniques book.

Download

18 Comments

  1. Dzoni45 says:

    Great quiz,
    I recognize that the longest answers are correct :)

  2. Vanessa says:

    This was so fun, and I'm not even a JS developer! You're stuff is really great, Martin! :)

    1. Martin Angelov says:

      Thank you! I am happy that you like it :)

      1. Paul Stanley says:

        Great work, Martin very easy to understand. -Paul

        San Jose, California

  3. Exploretheme says:

    Great job!

    You guessed 14 out of 18!

    :) :)

  4. MaxArt says:

    Now some are tricky... #13 above all, because nobody uses that anymore and you have to remember what it does.
    And #18 too, because you've either have seen it around (I did) or there's no way to tell what it does without trying it in a browser (or Node)!
    On #15: the answer doesn't exactly match the function's behavior, you may want to fix it ;)

    Entertaining, anyway. Thanks Martin :)

    1. Martin Angelov says:

      Thanks! It is perfectly fine to try things out in the browser before answering one of the tricky questions. It isn't called cheating, but learning :)

  5. kuus says:

    never seen the #13, which make sense since it's dead code, in fact closure compiler minify that like this:

    function whatDoesItDo(){var a=[];a.push("1");a.push("2");a.push("4");return a.join(",")};
    

    weird syntax though,
    thanks for the quiz

  6. Kamilius says:

    Awesome quiz. One of many things I like your web-site for :)
    Thanks!

  7. Radomír Žemlička says:

    The 13th one really got me. I didn't know you can break a labeled block, I thought it's just for loops. Also the 15th question. Until today I didn't know strings have these methods (probably because they're obsolete). But great quiz. ;-)

    1. Martin Angelov says:

      I also discovered that you can label blocks only recently (it works with any statement like loops and if). Their use is discouraged, but are supported, which makes them perfect for the quiz :)

      The string methods are non-standard but happen to be supported by all browsers.

  8. Martin says:

    15/18 love this kind of quiz, great job :)

  9. Jesús Bejarano says:

    Nice quiz, 14 of 18, realy didn't know that you can do some things with some objects.

  10. swapnil mhaske says:

    Saw a good js quiz after long time, learnt a few things myself.
    Thank you.

  11. Juan says:

    Loved it! Learned a lot too. 13/18

  12. Gavin says:

    This was great.

    I think it would make for a great follow up to go through a few of these and explain how they work.

    Thanks for the quiz

  13. Majid says:

    11 solution say :
    Returns a sorted copy of the array

    but it is not true :

    var arr1 = [2,4,23,123,6,87];

    function whatDoesItDo(arr) {
    return arr.slice(0).sort();
    }
    var sortedcopy = whatDoesItDo(arr1);
    console.log(sortedcopy);

    display :
    [123, 2, 23, 4, 6, 87]

    1. Doug says:

      that's how Array.sort() will treat integers by default.
      It is sorted list, just not an extremely useful one :)

      http://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly

Add Comment

Add a Reply

HTML is escaped automatically. Surround code blocks with <pre></pre> for readability.
Perks:   **bold**   __italics__   [some text](http://example.com) for links