Sunday, 20 April 2014

And the winner is.....

A very fancy bit of code - produces a winner
So 2 weeks ago I set a contest to win a Buzzle Ball. Entry to the contest required only that you make a comment under the blog post and follow up with an email using my Contact page to allow me to contact you again.

There were only a few entries. I had planned to use a rather complex system using an excel spreadsheet and a random number generator. A friend at work who is a bit of a whizz with Javascript gave me the following code which will take all the comments and exclude me and then randomly generate a winner.

function getCommentAuthors(from, selector){ var authorEls = from.querySelectorAll(selector), authorNames = []; [].forEach.call(authorEls, function(a){ authorNames.push(a.textContent); }); return authorNames.sort(); } var authors = getCommentAuthors(document.getElementById('comments'), 'cite.user:not(.blog-author) a'), randomisedAuthor = authors[Math.floor(Math.random() * authors.length)]; console.log(randomisedAuthor);
So a while ago I ran the code on that page using Chrome's javascript console and it produced the winner.

The winner is:
Michel van Ipenburg

Congratulations!
My commiserations to those who didn't win but the Buzzle ball will be available on-line soon.

2 comments:

  1. I'm one lucky guy tonight! Thanks Kevin!

    ReplyDelete
    Replies
    1. My pleasure!
      I'll post it later this week.

      Delete