Answering Nicholas Zakas' JavaScript quiz
A current meme that's floating around the JavaScript geek corner of the internet is setting quizzes
The FizzBuzz challenge has been around a while but I stumbled across it again after reading another unique Giles Bowkett post.
If you're not familiar with FizzBuzz, it's a little 'challenge' designed to test a candidate programmer's ability to perform a simple task. In this case, you just have to print out the numbers from 1 to 100, unless the number is a multiple of 3, when you should instead print "Fizz", 5 in which case you print "Buzz", or both 3 and 5 in which case you print "FizzBuzz".
Here's a trivial JavaScript implementation:
Pretty simple stuff, but a bit verbose. I wanted something that would fit into a tweet. It turns out that's pretty simple - this is 133 characters including whitespace, 7 within tolerance for a twitter message:
Which of course begs the question - just how short can a JavaScript FizzBuzz implementation be? Here's my baseline, which is a tortured and contorted version of the above:
The above is 71 characters - I expect you to do better. The rules are that the only dependency is Firebug's console.log being available, and you can't replace 'console.log' for anything else.
Of course, if we did swap 'console.log' for 'alert', the whole thing would fit in a tweet twice, but that would be damn annoying.
Hint: you can take at least three more characters off the above - can you see how?
After exploring the concise JavaScript FizzBuzz implementation, you might enjoy delving into Writing Compressible JavaScript, which offers strategies to optimize code for better compression and performance. Additionally, for a deeper understanding of JavaScript functions, consider reading 'function' in JavaScript - operator vs statement, which discusses different methods of defining functions in JavaScript and their respective nuances.
A current meme that's floating around the JavaScript geek corner of the internet is setting quizzes
Writing a library is a balancing act between the (sometimes competing) interests of API clarity, cod
I have an article on Behaviour Driven Development for JavaScript in June's edition of the excellent
In JavaScript we have at least 4 ways of defining functions: [code lang='js'] function myFunction()