Saturday, December 22, 2012

A simple Javascript Caculator demo



A simple JavaScript calculator demo:
1) Design a table, first part title "Calculator",  second part form for input  button
2) In the form, when a button is clicked. the value is added to the string in outputbox
3) When "=" button is clicked, the string in the output box is evaluated
4)  When "reset" button is clicked, the string in the output box is set to empty.
JSCaculator.js:
function calculate (form)
{form.outputbox.value = eval (form.outputbox.value)}

function addString(form, str)
{ form.outputbox.value += str}

function reset(form)
{form.outputbox.value = ""}


 JSCaculator.html:
 <html>
<head>
<title>A Simple JavaScript Caculator</title>
<script  type="text/javascript" src="JSCaculator.js">
</script>
</head>
<body>
<table  border="4"  bgcolor="#C0C0C0">
  <tr>
    <th><tt>
      <h2 align="center"><font color="#0000A0">Caculator</font></h2>
    </tt></th>
  </tr>
  <tr align="center">
    <th><form  name="myform">
      <table  border="1">
        <tr><td  colspan="4" align="center">
               <input  type="text" name="outputbox"  size="20">
           </td>
</tr>
        <tr align="center">
        <td><input type="button"  value=" 7 " onClick="addString(this.form,'7')">
</td>  
          <td><input type="button"  value=" 8 " onClick="addString(this.form, '8')"></td>
          <td><input type="button"  value=" 9 " onClick="addString(this.form, '9')"></td>
          <td><input type="button"  value=" / " onClick="addString(this.form, '/')"></td>
        </tr>
     <tr align="center">
          <td><input type="button"  value=" 4 " onClick="addString(this.form, '4')"></td>
          <td><input type="button"  value=" 5 " onClick="addString(this.form, '5')"></td>
          <td><input type="button"  value=" 6 " onClick="addString(this.form, '6')"></td>
          <td><input type="button"  value=" * " onClick="addString(this.form, '*')"></td>
        </tr>
     <tr align="center">
          <td><input type="button"  value=" 1 " onClick="addString(this.form, '1')"></td>
          <td><input type="button" value=" 2 " onClick="addString(this.form, '2')"></td>
          <td><input type="button" value=" 3 " onClick="addString(this.form, '3')"></td>
          <td><input type="button" value=" - " onClick="addString(this.form, '-')"></td>
        </tr>
     <tr align="center">
          <td><input type="button" value=" 0 " onClick="addString(this.form, '0')"></td>
          <td><input type="button" value=" . " onClick="addString(this.form, '.')"></td>
          <td><input type="button" value=" + " onClick="addString(this.form, '+')"></td>
          <td><input type="button" value=" % " onClick="addString(this.form, '%')"></td>
        </tr>
     <tr align="center">
          <td colspan="2">
              <input type="button" value=" = " onClick="calculate(this.form)"></td>
          <td colspan="2">
              <input type="button" value="Clear" onClick="reset(this.form)"></td>
</tr>
   </table>
    </form>
    </th>
  </tr>
</table>
</body>
</html>

Video for this demo:

No comments:

Post a Comment