Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

Coding Java Script Calculators For Your Web Site

Maybe you have a Web site where your visitor would find an online calculator very handy. Maybe you have a crafting instruction site where the visitor needs to calculate some dimensions. Or maybe you have an investment site where the visitor needs to calculate some money numbers. With a little Java Script code you can easily provide a custom calculator right on your Web page.

What makes this possible is Java Script's powerful built-in eval() function. All you have to do is provide the eval function with a mathematical expression in a string (computerese for a sentence inside quotes) and the eval() function will convert the string to the proper numbers and operators and return the result of the expression. For example, the statement below returns the number 12.

eval("2 * 6");

And if the eval() function isn't powerful enough for you, Java Script also has a built-in Math object that you can use to perform square roots, logarithms, trigonometric functions, and much more. You could build a full-featured scientific calculator for your Web page.

Below is a quick little basic calculator. Nothing fancy here. I'm sure you could easily make one with more powerful features that looks much better.


The process of building a calculator requires three main steps: 1. Build an html form; 2. Write Java Script functions to process the data from the form - basically using the eval() function; 3. Display the results of the calculation.

The calculator above uses only two small functions, enterNumber(digit) and calculate(operation). Two other tiny functions, clearEntry(form) and clearAll(form) are used to clear the calculator. When you click on one of the digit buttons, the buttons onClick event calls the enterNumber(digit) function as shown below.

<input type="button" name="7" value="7" onClick="enterNumber(this);">

function enterNumber(digit)
      newNumber = false;

   if(digit.value == "." && !decimalSet)
      digit.form.display.value = 
         digit.form.display.value + digit.value;
       decimalSet = true;
      digit.form.display.value = 
         digit.form.display.value + digit.value;

Note the "this" keyword passed to the enterNumber(this) function. This passes the button object to the function. Apparently each element of a form contains a property that is a pointer to the parent form object because you can use the button.form operator to access other elements in the form. If you didn't understand that last sentence, don't worry - neither did I!

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268