Baking up a Colour-Randomized Calculator from Scratch

While coding a calculator from scratch (using HTML/CSS/Javascript) I thought it would be good, wholesome fun for the buttons to change colour randomly. They probably make buttons on calculators boring for good reason. Once the random colour-changing was in place I found myself forgetting about the math and taking far too much pleasure in clicking buttons at random. Like a child.

Go ahead, try it out!

I thought this project was going to be way trickier than it turned out to be. Apparently you can pass an ugly string into the javascript eval() function and it does all kinds of magic (including treating strings as numbers and operators (like +-/x) as operators. It just crunches through and gives you an answer. This last bit is super helpful because I was trying to store an operator in a variable and getting all kinds of errors!

Eval() does its best to resolve whatever you throw at it. Even if you try something stupid like 5 – + 5 it simply ignores the first operator and adds the 5s together to give you 10.

Neat.

 

Leave a Reply

Your email address will not be published. Required fields are marked *