3 Feb 10 — 0

Weecode Snippets - Part 1

JavaScript really is the hobbyists language. Everyone has two or three run-time environments sitting around, it has some interesting language features and it is genuinely useful. Of course, that doesn't stop people like me coming up with generally useless things to do with it.

And so I present: Weecode. Weecodes are JavaScript snippets in 140 characters or less - conveniently short enough for a tweet! If you feel like contributing, send your wee functions to @weecode and I'll post them back. At some point there will be a page here where you can vote and tag snippets.

I'm looking for particularly tiny, clever or obscure code. The fewer 'higher level' constructs you use the better (who needs a pesky Math object, anyway?). Does it appear to solve quadratic equations while calculating the volume of a sphere? Even better.

The idea is to show off your abilities, have fun and maybe learn something new along the way.

To start things moving, I've posted two "useful" functions:

Figure out the nth Fibonacci sequence number - 66 characters:

function F(n){p=0;n*=2;while(n--){p-=4/((n--)*(n%4-1));}return p;}

Calculate sin of x in n iterations (n > 6 is basically as good as Math.sin) - 105 characters:

function sin(x,n){n*=2;r=0;while(n--){for(i=(f=n),p=x;--i;f*=i,p*=x){}r-=(n--%4-2)*p/f}return r;}

Post your comment

Comments for this page have been disabled

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments