Today's challenge is deceptively simple. Square a number. Given the input of an integer, output that integer multiplied by itself.
E.g.
square(3);
//output: 9
square(15);
//output: 225
Of course, I only want to see the worst ways to do this. Don't hold back. Make the compiler beg for mercy! You might find inspiration in the Wikipedia article on squaring.
Tests
square(4) == 16;
square(16) == 256;
square(51) == 2601;
Latest comments (29)
Late, but any excuse for a cross join:
We need some bloated JavaScript libraries.
Not a full code snippet, but I would submit something terrible like this:
Just ignore the floating point error:
Create an x * x array of dots. Count the dots.
AFK, but the method I want to try is just a series of additions;
n^2 - (n-1)^2 == n + n-1
, so just do that in a for-loop from 1 to n.Simple enough. Just create some random square images and check randomly to see if the diagonal of one equals our number, with the added bonus of a potential for an awesome looking image.
And by the Infinite Monkey Theorem, given an infinite number of monkeys running an inifinite number of square functions, we'll eventually have the worlds greatest image. What a bonus.
I think I found another one, even better:
this one works by searching inside the comments to this post any solution that was written in javascript (looking if the author has selected the js highlighter), then simply
eval
s it and finally runs itsince it could be picked itself, it must be named square
this is my favorite answer
So meta 😂
thanks
Obviously, to square a number you first need to draw a square.
CSS
JS
Limitations
It will only work for non-negative integer values of
n
.Other than that, it's limited only by your browser's ability to render thousands upon thousands of
div
s.I was hoping I'd see something like this 😁
Beg for mercy.