OK, Dustin Diaz (via Paul O’Shannessy) managed to delay my lunch by 30 secs.

var arr = ['a', 'b', 'c', 'c', 'd','e', 'e',
'e', 'e', 'e', 'f', 'e', 'f', 'e',
'f', 'a', 'a', 'a', 'f', 'f', 'f'];
var solution='a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f <span>f</span>';
var won = arr.join(' ').replace(/((\w)\s\2\s)((?:\2\s?)+)(?=\s|$)/g, '$1<span>$3</span>') == solution;

Thanks to the wonderful interactive environment of Firefox's "Error Console" (formerly known as JavaScript Console), my pasta didn't get too cold :)

8 Responses to “Programming Brain Teaser”

  1. #1 Sam Hasler says:

    How did you use the Error console? That would be a useful trick to learn. I tired pasting your code into it and clicking on Evaluate but it didn't do anything.

  2. #2 Giorgio says:

    @Sam Hasler:
    You need to enter all in one line (unless you store variables in a window object, e.g. using "top.arr") and end the script with an expression but not an assignment, e.g.:

    var arr = ['a', 'b', 'c', 'c', 'd','e', 'e','e', 'e', 'e', 'f', 'e', 'f', 'e', 'f', 'a', 'a', 'a', 'f', 'f', 'f']; var solution='a b c c d e e <span>e e e</span> f e f e f a a <span>a</span> f f <span>f</span>'; arr.join(' ').replace(/((\w)\s\2\s)((?:\2\s?)+)(?=\s|$)/g, '$1<span>$3</span>') == solution;

    (in one line) will give "true" as its output.

    A portable, concise bookmarklet to quickly test multiline JS is the following:

    javascript:document.body.innerHTML="<textarea onchange='eval(this.value)' cols=80 rows=6/>";void(0)
  3. #3 Paul O'Shannessy says:

    I like it. Clever use of regular expressions.

  4. #4 Sam Hasler says:


  5. #5 john says:

    pls apologize my stupidity.. but what will this expression do?

  6. #6 therube says:

    For whatever reason, I am not able to get your bookmarklet to work?
    No matter how I've tried it, I end up with an empty box at the top of the screen?

    If I copy/paste into the URL line (prefaced by javascript: ), I correctly get "true".

    I had this bookmarklet (which I picked up somewhere?) that I called 'Execute HTML'. If I recall, I would use it from time to time to execute HTML snippets. But when I try it on on the script, again I end up with a blank page & no "true".

    javascript:(function(){var sel;function getText(w){if(w.getSelection){return w.getSelection();}if(w.document.getSelection){return w.document.getSelection();}return w.document.selection.createRange().text;}function getSel(w){try{sel=getText(w);if(sel)return;for(var i=0;F=w.frames[i];i++){getSel(F);if(sel)return;}}catch(e){}}getSel(window);if(!sel)alert('Select some text.');else{w=open();w.document.write(sel);w.document.close();}})()

    Nothing here either, http://www.javascriptwindow.com/ ?

  7. #7 Giorgio says:

    My script has no output per se.
    If you want it to show a result, add this line:

  8. #8 piju says:

    i am not a programmer,
    so i dont know what is this,
    what i see about you is
    you are such a great guy
    u own an IT company,
    u wrote great stuff
    and the most is,
    u are italian!


    i love pizza

Bad Behavior has blocked 1403 access attempts in the last 7 days.