A JavaScript challenge for Nordic.js

by Peter Jaric

Today and yesterday I’ve been visiting Nordic.js, a Scandinavian JavaScript conference. To celebrate the occasion I made a little JavaScript challenge that I announced at Twitter:

challenge

I had a specific solution in mind, which I didn’t receive, but I got a load of other solutions instead:

The first one was from Andreas Madsen and it was in line with what I was looking for: changing the text between /* and */.

andreas_madsen

Then Jonas Magazinius bent the rules (rules that only existed in my mind) with this “feature flag” solution:

jonas

This was Mathias Bynens‘ first solution. It was similar to Andreas’ code, but implemented a bit differently.

mathias1

From Joshua Adams ‏I got this solution that required an HTML context, but was kind of cool anyway:

joshua

Mathias sent in some more proposals, getting closer to expected solution, the second being IE 10 only if I got it right. Update: Well, that was wrong, it actually is valid JavaScript. See this page about comment syntax.

mathias2

mathias3

While writing this post I got this from File Descriptor, and this was on par with what I was looking for, using the JavaScript feature “Automatic Semi-colon Insertion” and line terminators in a comment:

filedescriptor

Finally, the code I wrote that inspired this challenge.

var a = 0, b = 0;
a/*
*/++/*
*/b

vs.

var a = 0, b = 0;
a/**/++/*
*/b

On Twitter I wrote that I would declare the “coolest” solution the winner, and I think it’s the one from File Descriptor. Congratulations!