★ wanayoo — archive 1999 http://javascript.about.com/compute/javascript/library/weekly/aa081999.htmNouvelle recherche | Portail wanayoo
Enter Sweepstakes! · Advertise on this site · Get Bargains
About.com  
You are here:  About > Computing/Technology > Focus on JavaScript
an About site
   About.com
 
 One of Over 700 Sites
Your Guide with Guide
Nate Kassebaum
Bio | Contact

Subjects
Compatibility
Reference
Animation Scripts
Cookie Scripts
Image Scripts
Text Scripts
Frame Scripts
Form Scripts
HTML Scripts
Navigation Scripts
Password Scripts
Time Scripts
Window Scripts
Software
Beginner Tutorials
Cookie Tutorials
Image Tutorials
Text Tutorials
Form Tutorials
Frame Tutorials
Multimedia
Navigation Tutors
Time Tutorials
Handling Variables
Window Tutorials

Subject Library 

All articles on this topic

Bookstore
Find books related to this topic Click Here

Videostore
Find videos related to this topic Click Here

ShoppingAbout
Your favorite products, right here Click Here

Stay up-to-date!
Subscribe to our newsletter.

Do you like our sites?
Wish to share them with others - and earn money?
Become an Affiliate

More Sites On This Topic
Apply to become
a partner
for this site.

Search for 
in   Sites A to Z
 
 

A JavaScript Calculator

Dateline: 08/19/99

Here is a quick example of a few essential JavaScript topics.

The calculator above does pretty much what you would expect it to do. You enter a number, an operator, another number, press equals, and it gives you an answer.  Let's take a look at how this works.

First, we've got all the numbered buttons.  The HTML code for these buttons looks like this:

<input type="button" value=" 1 " onClick="calcNumber(1)">
<input type="button" value=" 2 " onClick="calcNumber(2)">

... and so on...

Then we've got the display.  This is the text box that the numbers appear in when you press the buttons.  The HTML code for that looks like this:

<input type="text" name="calcDisplay">

All of these elements are wrapped inside of a <form> tag, that looks like this:

<form name="calcForm">

When you click the numbered buttons, the calcNumber() function is called, which looks like this:

function calcNumber(thisNum) 
{
 document.calcForm.calcDisplay.value += ("" + thisNum); 
}

All this does is appends the number pressed to the end of the calculator display.  (Note: The "" + thisNum is necessary to convert thisNum from a number to a string.  If the "" part is omitted, the function would perform arithmetic addition instead of string addition.)

So far, pretty simple.  Next, we have the operator buttons.  The HTML code for these looks like this:

<input type="button" value=" + " onClick="calcOperator('+')">
<input type="button" value=" x " onClick="calcOperator('*')">

... and so on ...

When you click on one of these buttons, the computer needs to remember what operator you chose (so it can perform the correct operation when you click the equals button).  In order to do this, I have included a hidden form element called "calcOperatorMemory."

<input type="hidden" name="calcOperatorMemory" value="">

Also, after you click on an operator, the computer will need to remember what number you entered so you can enter another number.  This is handled in a similar way, with a form element called "calcNumberMemory."

<input type="hidden" name="calcNumberMemory" value="">

The calcOperator() function assigns values to calcOperatorMemory and calcNumberMemory so that the computer knows which operator you clicked on and which number was entered.  The calcOperator() function looks like this:

function calcOperator(thisOp) 
{
 document.calcForm.calcOperatorMemory.value = thisOp;
 document.calcForm.calcNumberMemory.value = document.calcForm.calcDisplay.value;
 document.calcForm.calcDisplay.value = ""; 
}

Finally, there is the equals button, which looks like this:

<input type="button" value=" = " onClick="calcEquals()">

The calcEquals function checks to see which operation was selected, and performs that operation on the number in calcNumberMemory and the number currently in the display.  The result is then written to the display.  Here is the calcEquals function:

function calcEquals()
{
 if (document.calcForm.calcOperatorMemory.value == '/')
  {
  document.calcForm.calcDisplay.value = document.calcForm.calcNumberMemory.value / document.calcForm.calcDisplay.value;
 }
 else if (document.calcForm.calcOperatorMemory.value == '*')
 {
  document.calcForm.calcDisplay.value = document.calcForm.calcNumberMemory.value * document.calcForm.calcDisplay.value;
 }
 else if (document.calcForm.calcOperatorMemory.value == '+')
 {
  document.calcForm.calcDisplay.value = (document.calcForm.calcNumberMemory.value - 0) + (document.calcForm.calcDisplay.value - 0);
 }
 else if (document.calcForm.calcOperatorMemory.value == '-')
 {
  document.calcForm.calcDisplay.value = document.calcForm.calcNumberMemory.value - document.calcForm.calcDisplay.value;
 } else {
  alert ("You must choose an operator.");
 }
}

Here is the complete cut-n-paste source code:

Visit the message boards!

Previous Features



Email this page!

Sponsored Links
Netsetter - the free download accelerator
Download and install the Netsetter software and you can double your internet speed and have a chance to win a BMWZ3 Sports Car!
http://www.commission-junction.com/  (Listing fee: $0.33)
Find Practically Anything on Earth at eBay!
eBay - the world's largest online trading site where people buy, sell or trade practically anything on earth. Over 4 million items available in over 4,000 categories including javascript
http://cgi.ebay.com/  (Listing fee: $0.30)
Increase your download speeds for FREE!
Netsetter will optimize your web browser and internet connection settings to increase your internet speed by up to 100%.
http://www.commission-junction.com/  (Listing fee: $0.23)
Web Design for Search Engines - Web Position Gold
Web design & promotion software for higher search engine ranking. WebPosition Gold optimizes, submits, & tracks your site for top results. FREE Trial download.
http://www.web-gold.net/  (Listing fee: $0.22)
Computer Training Videos & CD ROMs
Planetlearn.com provides over 2000 self paced computer training programs available on video, cd rom, and online.
http://www.planetlearn.com/  (Listing fee: $0.21)
Sponsor this site More...


 
 Advertising
AllBusiness.com
Create your business plan. Here’s how.

win millions!!
Just three easy steps can make you a millionaire!

HotJobs.com
Better Jobs for a Better Life

Win a piece of *NSYNC!
Head to Alloy.com for a chance to score your very own platinum copy of No Strings Attached!

single pixel
single pixel
Marketplace
register domains
get a website$499
long distance deal
- - free stuff - -
meet your match
virtual makeover
high paying jobs
quick jobs search
free ebiz solutions
meet your match
build a business
$50 off mp3 player
meet your match
book club
longdistancedeal

 
Related sites
single pixel
on About 
Focus on Java
Focus on Web3D
HTML
Perl
Personal Web Pages
Web Design
XML


Explore More On The About Network
Search
Arts/Humanities ·  Autos ·  Cities/Towns ·  Comedy ·  Computing/Technology ·  Cultures ·  Education ·  Food/Drink ·  Gadgets ·  Games ·  Health/Fitness ·  Hobbies ·  Home/Garden ·  Homework Help ·  Industry ·  Internet/Online ·  Jobs/Careers ·  Kids ·  Money ·  Movies ·  Music/Performing Arts  ·  News/Issues ·  Parenting/Family ·  People/Relationships ·  Pets ·  Recreation/Outdoors ·  Real Estate ·  Religion/Spirtuality ·  Science ·  Shopping ·  Small Business ·  Sports ·  Style ·  Teens ·  Travel ·  TV/Radio · 
 
single pixel

For more information, visit
Our Story, Be a Guide, or Advertise. For rules of use,
read our User Agreement and
Privacy & Kids Privacy Policy.

Having a problem?
Report it here.

About Canada   ·    About UK

 
Copyright  © 2000 About.com, Inc.
About and About.com are registered trademarks of About.com, Inc.
The About logo is a trademark of About.com, Inc.
All rights reserved.