This Message Forum is no longer in use

Please use the new Bravenet Help Forums FOUND HERE

General Forum
This Forum is Locked
Author
Comment
javascript calculator

i'm having a bit of trouble with a project that i'm
working on- a javascript calculator- and after
banging my head on the keyboard for a few days i
thought i'd upload it to my test site and ask the
bravenet world. here it is:
http://freespace.virgin.net/tomz.willoughby/calculator.htm
thx in advance

Re: javascript calculator

Well I hate to say it, but, you have a lot of learning to do. You are using some Javascript correctly and then others seem like just a guess. You are over thinking some operations, and under thinking others.

I have place a working version of your calculatior on my web site at Scutternam's Calculator. You can use your right mouse button to view/save the source code and compare it against your original code. You will note that I added a clear function and greatly simplified the other functions. Note the structure of "if/else if" statements in the Operate() function. That's one area you really over thought and structured incorrectly.

Here is description of the code:


  1. Initially the answer box is assumed to be empty.
  2. Selecting a number key (0-9) will start to assemble the first number. As each key is pressed the assembled number is displayed.
  3. Selection of a operation (+,-,*,/) will terminate the assembly of the first number and allow the start of the second number. The operation will be displayed on the right of the first number, separated by a space.
  4. Selecting a number key (0-9) will start to assemble the second number. As each key is pressed the assembled number is displayed next the the first number and the operation, separated by a space.
  5. Selection of the equal operation (=) will perform the selected operation on the first and second number. The answer will be displayed. The variables are cleared in preparation for a new calculation ( This effectively a return to step 2).
  6. Selection of the clear button (CL) will reset all the variables and clear the display area (This is effectively a return to step 1).

The code is pretty straight forward, and I really don't want to go into any more detail in this forum, but if there is anything you don't understand, let me know. Just follow the "Contact Me" link on my web site. Here are some of my suggestions.

  • If you want to write Javascript, get a browser with a debugger that is easier to work with than Internet Explorer. IE is terrible to use, but Netscape has a nice Javascript Console.
  • Use lots of comments. When you are dealing with small test files, things are pretty easy to work with. But when you get into Javascript programs that have hundreds of functions and thousands of lines, you better have it well documented. Get use to it early.
  • Learn your data types, and always be aware of which variable is holding which data type. That may not seem to make a lot of sense, until you try to add a variable of data type float to a variable of data type string. This can be done, but the result may not be what you expect.
  • Be very aware of the limitations of Javascript. Initially, simple things, like not having a function for rounding off a number before it's output. And then onto to details, like not being able to do a Log, base 10, of x. The Javascript Log function is natural log (base e). If you want any of these functions, you have to write them yourself.
  • Learn the difference between local and global variables, and how to pass them in a function call. This seems like a pretty simple thing, but there are a lot of places that can give you problems. Like trying to use a global variable as a function parameter. That one will keep you scratching your head for hours.
  • Test, Test, Test. Try to think of every way a user can mess up. Like in the calculator code, what happens if the user selects another operation key, while they are in the middle of entering the second number?
  • Yad da da Yad da da Yad da da

There is lot's more to be added to this list, but they are details that you will learn as you go. I don't profess to know everything about Javascript, but if your stuck, let me know, and I will check it out.

Re: Re: javascript calculator

Thanks for your help. I know i'm only a novice with javascript but your comments realy help. I did get the calculator working for one number and multiplication but then I decided to put the numbers into a function and it all fell apart.

thx again,
Scutterman