Calculate two values in JavaScript

December 10, 2009 JavaScript

Calculate two values in JavaScript

<Script Language="JavaScript">

function Calc(){
var a,b,result;
var v1,v2;
a=frm1.txt1.value
b=frm1.txt2.value
b=frm1.txt2.value
c=frm1.txt3.value

v1=parseInt(a);
v2=parseInt(b);

switch(c){

case "+":
result=v1+v2;
frm1.txt4.value=result;
break;

case "-":
result=v1-v2;
frm1.txt4.value=result;
break;

case "*":
result=v1*v2;
frm1.txt4.value=result;
break;

case "/":
result=v1/v2;
frm1.txt4.value=result;
break;

case "%":
result=v1%v2;
frm1.txt4.value=result;
break;

case "":
alert("Please Enter any Operator")
break;

default:
alert("Please Enter the Specific Operator")
}
}

function resetBtn(){
frm1.txt1.value=""
frm1.txt2.value=""
frm1.txt3.value=""
frm1.txt4.value=""
}

</Script>
</head>
<body>
<form name="frm1">
&nbsp;<p>
Value 1
<input type="text" name="txt1" size="20"> </p>
<p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Operator&nbsp;
<input type="text" name="txt3" size="5"></p>
<p>
&nbsp;Value 2 <input type="text" name="txt2" size="20"></p>
<p>
&nbsp;Result&nbsp;&nbsp;&nbsp;
<input type="text" name="txt4" size="20"> </p>
<p>
&nbsp;<input type="button" name="btn1" value="Calculate" onClick="Calc();">
<input type="button" value="Reset" name="btn2" onClick="resetBtn()"></p>
</form>

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Tags:

Leave a Reply