PETEYMAC.COM

Basic JavaScript Calculator

In an attempt to learn JavaScript, I bought this JavaScript book which I saw at one of my favorite places to buy books, Half Priced Books. I brought the book home and spent a long weekend trying to learn it. Since I already know Java, I thought it would be pretty simple, but the two language aren't all that similar. You can click on the pic of the book if you are interested in buying it on Amazon.

JavaScript Book

One of the exercises that seems to be in every computer programming book is the "build your own calculator" program. So I sat down and diligently typed the code that was in the book and of course, it didn't work. Twenty Four hours later (hence the name of the book - 24 Hour Trainer) and debugging a whole bunch of typo's and figuring out exactly what the JavaScript actually did, I had a VERY basic calculator that performed addition, subtraction, multiplication and division. I typed in "2+2" took a deep sigh, hit the equal sign and let out a shout that the answer "4" actually came up. I was so excited but then found it wasn't very useful because it didn't have a decimal point. So, I decided that I would add a decimal point button, which required another column on the calculator. Needing three more functions, I added the left and right parenthesis, so I could do more complex functions. With a button left, I added the "MOD" function. MOD is a function the returns the remainder when one number is divided by another; so, 12 MOD 5 (displayed as 12%5 in the calculator) would be 2.

Enjoy!

1 2 3 + mod
4 5 6 - (
7 8 9 * )
clear 0 = / .