JavaScript Challenge #2 (Whac-A-Moo)

Written By Christopher Pitt, on Wednesday, August 1st 2012, 4:49am

Hopefully the first challenge got you excited for JavaScript development in a competitive environment…

One of the most common requirements of web applications is to be able to respond to user input, and in particular clicks (or touches). Continuing with our theme of element generation (and in some cases animation); this week’s challenge will be to create a basic whac-a-mole game.

The point of the game will be to click on highlighted elements to increase score. This can be as basic or as intricate as you wish it to be, but it should resemble the following diagram…

The green block represents the block a player must click in order to gain points. This means you will need logic to randomly select one of the possible blocks and highlight it. You also need to register user clicks and update the score “label”.

Hint: at times when you need to add many of the same kind of event to a variable amount of elements (in this case blocks); it is helpful and even efficient to use event delegation. You will need to use the setInterval() function or the setTimeout() function to create a predictable amount of time between each click or highlight.

As before; post your solutions in the form of links to jsfiddle, tinker or jsbin (in the comment section) and we’ll update this post with a link to a review post detailing solutions that we think tackle the problem in ways worthy of mention.

EDIT [2012/08/08]: We are closing comments and reviewing submissions. We will follow this post up with a review of our favourites and what they teach us about the task and JavaScript development in general.

EDIT [2012/08/22]: We have posted the review post to for these submissions.

19 Responses to “JavaScript Challenge #2 (Whac-A-Moo)”

  1. rud says:

    https://tinker.io/0ae5b

    note, difficulty increases over time based on your current score.

    have phun.

  2. rud says:

    okok, arian / chiel exploited the thing in two secs, second iteration:

    https://tinker.io/0ae5b/1

    note, difficulty increases over time based on your current score.

    have phun.

  3. Christopher Pitt says:
  4. rud says:

    bah, new release up..

    figured the game was still too easy to play, so i added a time limit to the game and play/stop button + some cosmetics

    https://tinker.io/0ae5b/6

    have phun.

  5. cpojer says:

    Can you beat https://tinker.io/66bb4/4 and win one of the grand prizes?

  6. haliphax says:
  7. Kevin says:

    I think this is the only solution with tables so far: http://jsfiddle.net/hgPZM/

  8. Lionel Garcia says:
  9. Kevin says:

    I ported my solution to Dart for fun: https://gist.github.com/3229945

    Here it is converted to js: https://tinker.io/58980

  10. Kevin says:

    Does tinker.io have a js size limit? Apparently some of the JS is getting cut off. This is the converted JS: https://gist.github.com/3229987

  11. Pete says:

    Went for “short” this time. 17 lines of code. Not sure I could make it much shorter:

    http://jsfiddle.net/tqvMc/

    Again, I am probably losing points for using jQuery. But I haven’t had the pleasure of becoming a MooTools pro yet.

  12. Alkadia says:

    This is my implementation in plain javascript:

    http://jsfiddle.net/Alkadia/QTnQa/

    Note: inside the class, the blocks generator is inherited by the previous challenge.

  13. Chiel says:

    @Kevin, tinker uses the field type of TEXT in the mysql db, to store the javascript. That apparently has a limit of 65,535 bytes. Nobody else has ever complained about it, but I’ll look into it.

    Tinker will probably switch to some kind of NoSQL system soon, which works entirely different.

  14. Chidge says:

    Here’s my contribution in vanilla JS.

    http://jsfiddle.net/chidge/yKHtY/17/

  15. cpojer says:

    This is the variant I originally wanted to do: https://tinker.io/c341c/8 Now you can hit the MooTools devs! Bonus points if you can add sounds :)

  16. Chidge says:

    (Working link for my fiddle!)

    Here’s my contribution in vanilla JS:

    http://jsfiddle.net/chidge/ufrh6/

  17. Olmo says:

    lmao @cpojer. brilliant!

  18. kentaromiura says:

    Ok guys, I didn’t have too much time this time, so I started from cpojer latest entry: https://tinker.io/c341c/8, I added some sound effects, then I added a background music, then arian fixed a little bug, then I though, why not adding the possibility to switch who to hit? so I implemented a quick and dirty (and rather ugly) twitter lists loader then dimitar (coda) though it was a good idea to show a preview of who you are gonna to hit with the hammer and improved the style a bit. I lastly added a negative score if you miss (but not if you are in the middle of two holes). and a time attack mode. I think what it miss now is a centralized leaderboard. But for this challenge it should be ok :)

    So, to recap, this is a collective work of cpojer, kentaromiura, arian, coda :

    http://jsfiddle.net/kentaromiura/rUqJ7/21/

    Enjoy ;)

  19. Dimcho says:

    Hi.

    Here is my solution ( no images, little CSS and vanilla JavaScript ;) ): http://jsfiddle.net/Dimcho/JmVbD/

    BR