JavaScript Code for a Simple Calculator

My last blog post I created a Drake calculator. As promised here is the code to it. This can be easily converted to any calculator. There is a few parts that are embarrassingly bad coding. So scrap what you need.

JavaScript Code

<script type="text/javascript">
var apple = new Array();
var count  =7; // number of values for loop
var answer = 0; // store final answer
var numericExpression = /^[0-9]+$/; //check for numbers in boxes
var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/; // check for floats
function testResults (form) {
apple[0] = form.A.value;  // place user values in the array
apple[1] = form.B.value;
apple[2] = form.C.value;
apple[3] = form.D.value;
apple[4] = form.E.value;
apple[5] = form.F.value;
apple[6] = form.G.value;
for(i =0; i<count; i++){  // loop through array to check for errors
    if(apple[i].length == 0){  // check to see if all fields are filled out
       alert("a value is empty"); // alert box pops up to inform user
       return false; }  // stops the process
    if(!apple[i].match(numericExpression) && !apple[i].match(isDecimal_re) ){ // checks for numbers
        alert("a box has letters in it");
        return false;	}  }
answer = (apple[0] *apple[1] * apple[2] * apple[3] * apple[4] * apple[5] * apple[6]); //multiply values
form.answer.value = answer;  } // displays answer

HTML code

<form method="GET">
<input name="A" type="text" value="10" />
<input name="B" type="text" value=".5" />
<input name="C" type="text" value="2" />
<input name="D" type="text" value="1" />
<input name="E" type="text" value=".01" />
<input name="F" type="text" value=".01" />
<input name="G" type="text" value="10000" />
<input onclick="testResults(this.form)" name="button" type="button" value="Click" />
<input name="answer" type="text" value="waiting" />

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on
Fabulou wholesale Petr Mrazek jersey is back. Best choice to get the wholesale Petr Mrazek jersey is suitable for all style of people.