Resources » TypeScript » Scripting Languages » Javascript

Simple calculator


Posted Date: 23-Sep-2008  Last Updated:   Category: Javascript    
Author: Member Level: Silver    Points: 7



Implement a simple calculator of your own in your website , use thoe following code inside your " body " tag , here functions for addition , subtraction , multiplication and division are written seperatly and is called when the appropriate button is pressed .


<form>
<table border=2 cellpadding=3 bgcolor=#E6E9EA><tr><td>
<div align="center"><b>Enter 2 Numbers:</b><br>
<input type=text size=5 name=num1>
<input type=text size=5 name=num2></div></tr></td><tr><td>

<div align="center"><b>Click on a Function</b><br>
<input type=button name=a1 value=" + " onclick="addFields(this.form)">
<input type=button name=a2 value=" - " onclick="subFields(this.form)">
<input type=button name=a3 value=" x " onclick="mulFields(this.form)">
<input type=button name=a4 value=" / " onclick="divFields(this.form)"></div>
</td></tr><tr><td>

<div align="center"><B>The Answer is:</B><br>
<input type="text" size=8 name="result"></div></td></tr><tr><td>
<div align="center"><input type=reset name=clear value="clear fields"></div></td></tr></table>
</form>
</td></table>

<script language="javascript">


function addFields(form) {
var sum = eval(form.num1.value) + eval(form.num2.value)
form.result.value=sum
}
function subFields(form) {
var sub = eval(form.num1.value) - eval(form.num2.value)
form.result.value=sub
}
function mulFields(form){
var mul = eval(form.num1.value) * eval(form.num2.value)
form.result.value=mul
}
function divFields(form) {
form.result.value = eval(form.num1.value) / eval(form.num2.value)
}
</script>



Did you like this resource? Share it with your friends and show your love!

Responses to "Simple calculator"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    hema malini
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India