Home arrow HTML arrow Page 3 - 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 - The showNum() Function
(Page 3 of 5 )

This is the skeleton of the showNum() with its associated global variable:


var showNumCalledOnce = false;


function showNum(ID)

{

//remove the initial zero in the Input Control unless the user began by typing 0 or the decimal point.


//Clear the display (Input Text Control) if -, +, X or / button has been clicked


switch (ID)

{

case "B0":

//display zero or add it on the right of a previous digit.

break;

case "B1":

//display 1 or add it on the right of a previous digit.

break;

case "B2":

---

break;

case "B3":

---

break;

case "B4":

---

break;

case "B5":

---

break;

case "B6":

---

break;

case "B7":

---

break;

case "B8":

---

break;

case "B9":

---

break;

case "BPOINT":

//display the decimal point or add it on the right of a previous digit.

break;

case "BRM":

//Store the number on the display

break;

case "BPM":

//Show the number stored on the display.

break;

}


//remove any leading zeros before you process a number.

}



When the calculator is opened, the initial number on the display is zero. When the user then types a digit, such as 5, you should see 5 and not 05. The global variable, showNumCalledOnce, is used to remove that preceding zero.

The above function has as argument, the ID of the button pressed. Read though the skeleton, taking note of the comments. There is a switch statement. When a digit button is clicked, it calls the above function, sending its ID as argument. The switch statement uses the ID to display the corresponding digit.


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials