Home arrow HTML arrow Page 5 - Coding a Web Page Calculator
HTML

Coding a Web Page Calculator


Welcome to the second part of a six-part series on how to build a calculator for your web page. In the previous part, we got into the basics of setting up the calculator's layout and what functions we want it to have. In this part, we will begin to code the functions.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
March 11, 2009
TABLE OF CONTENTS:
  1. · Coding a Web Page Calculator
  2. · Code Basics
  3. · The showNum() Function
  4. · The operator() Function
  5. · Explanation of the operator() Function

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Coding a Web Page Calculator - Explanation of the operator() Function
(Page 5 of 5 )

JavaScript has a top level function called the eval() function. This function takes a string as argument. The string can be an expression such as an arithmetic expression, e.g. “2 + 3.” Under this condition, the eval() function evaluates the arithmetic expression. Our standard calculator basically performs arithmetic expressions. We shall use the top level eval() functions to do all of our evaluations.

The evalString global variable is the string to be evaluated. It is the argument for the eval() function. The string is developed as the user is typing information into the calculator.

The store, or what the user calls memory, is simply a variable, the memVar global variable. When the user clicks the M+ button to store the content of the display to the memory, the content of the display simply becomes the value of the memVar variable.

The global variable toClearDisplay is a Boolean variable. Its default and initial value is false. When it is true and you click a digit, the display is first cleared for the new digit to appear on the display. The fourth user’s requirement above states that when you click the +, -, X or / button, the next number typed replaces the number on the display. The toClearDisplay variable is used for that; we shall see the details soon.

To explain the rest of the operator(ID) function, we shall assume that the user wants to evaluate the arithmetic expression 2 + 3. So after the calculator has opened, we assume that the user clicks 2. The digit 2 appears on the display (this is the result of the showNum(ID) function). Next he clicks the + button. The + sign does not appear on the display; this is how physical calculators behave. As soon as he clicks the + button, the operator(ID) function is called for the first time.

When the operator(ID) function is called for the first time, it develops the first part of the evaluation string, which is the left-most part. To do this it simply reads the content of the display. In our example, the evalString global variable would then be “2” instead of “”, which is its initial state. After this, the execution of the operator(ID) function will continue with the switch statement.

The operator(ID) function is called when the user clicks any of the non-digit buttons. The ID of the button is sent as an argument to the operator(ID) function. The ID is used by the switch statement. Continuing with our example, if the ID is BP, it means the + button was clicked. This corresponds to ‘case “BP”:’ in the switch statement. The code for this case adds “ “ to the evaluation string, and then it adds “+” to it, and finally it adds “ “ again. The evalStrnig now becomes “2 + ”.

Continuing with our example, the user now clicks the digit 3. The previous number 2 on the display disappears and the digit 3 appears (this is the result of the showNum(ID) function). Then the user clicks the Equal Sign button. The operator(ID) function is called with the ID of “BEQ” for the clicked button. The first code segment of the operator(ID) function adds the number 3 from the display to the evalString.

The evalString is now “2 + 3” and ready for evaluation. After this, the code for the case statement for the equal sign calls the eval(evalString) top level function. The content of the evalString, which is now “2 + 3,” is evaluated. The code for this case writes the result of the evaluation to the display (namely 5), replacing the previous number (3). This code also resets the evalString to “”. As such, you can use the result on the display as the start number for another evaluation.

The cases for the -, X and / operation can be similarly explained. I will explain the cases for the M+ and C buttons in the next part of series, where I will be treating the details of the two main functions. I will not treat it now to avoid unnecessary repetition.

Let us stop here, for this part. I'll see you for the next part.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
HTML ARTICLES

- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5
- BBC Adopts HTML 5, Mozilla Addresses Issues
- Advanced Sticky Footers in HTML and CSS
- HTML and CSS Sticky Footers
- Strategy Analytics Predicts HTML5 Phones to ...
- HTML5 Guidelines for Web Developers
- Learning HTML5 Game Programming
- More Engaging CSS3 and HTML Background Effec...
- Engaging HTML and CSS3 Background Effects
- More Web Columns with CSS3 and HTML
- Columns with CSS3 and HTML
- Creating Inline-Block HTML Elements with CSS
- Drag and Drop in HTML5: Parsing Local Files

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials