Calculator in JavaScript
<html>
<body>
<center>
<form name="keys" action=
"">
<table>
<B>
<table border=2
width=50 height=60
cellpadding=1
cellspacing=5>
<tr>
<td colspan=3 align=middle>
<input name="readText"
type="Text" size=24
value="0"
width=100%>
</td>
<td
</td>
<td>
<input name="buttonClear"
type="Button" value=" C " onclick=
"Clear()">
</td>
<td><input name="buttonClearEntry"
type="Button"
value=" CE " onclick="ClearEntry()">
</td>
</tr>
<tr>
<td>
<input name="btnSeven"
type="Button"
value=" 7 " onclick=
"number_press(7)">
</td>
<td>
<input name="btnEight"
type="Button" value=
" 8 " onclick=
"number_press(8)">
</td>
<td>
<input name="btnNine"
type="Button" value=" 9 " onclick=
"number_press(9)">
</td>
<td>
</td>
<td>
<input name="btnNeg"
type="Button" value=
" +/- " onclick=
"Neg()">
</td>
<td>
<input name="btnPercent"
type="Button" value=
" % "
onclick="Percent()">
</td>
</tr>
<tr>
<td>
<input name="btnFour"
type="Button" value=
" 4 " onclick=
"number_press(4)">
</td>
<td>
<input name="btnFive" type="Button" value=
" 5 "
onclick="number_press(5)">
</td>
<td>
<input name="btnSix" type=
"Button" value="
6 "
onclick="number_press(6)">
</td>
<td>
</td>
<td align=middle><input name=
"btnPlus" type="Button"
value=" +
" onclick="Operation
('+')">
</td>
<td align=middle><input name=
"btnMinus" type=
"Button" value=" -
" onclick="Operation
('-')">
</td>
</tr>
<tr>
<td>
<input name="btnOne"
type="Button"
value=" 1
"
onclick="number_press(1)">
</td>
<td>
<input name="btnTwo"
type="Button"
value=" 2 "
onclick=
"number_press(2)">
</td>
<td>
<input name="btnThree"
type="Button"
value=" 3 "
onclick=
"number_press(3)">
</td>
<td>
</td>
<td align=middle><input name=
"btnMultiply" type="Button"
value=" *
" onclick="Operation('*')"
>
</td>
<td align=middle><input name=
"btnDivide"
type="Button"
value=" /
" onclick=
"Operation('/')
">
</td>
</tr>
<tr>
<td>
<input name="btnZero"
type="Button" value=
" 0 " onclick=
"number_press(0)">
</td>
<td>
<input name="btnDecimal" type="Button" value=
" . " onclick=
"Decimal()">
</td>
<td colspan=3>
</td>
<td>
<input name="btnEquals"
type="Button" value=
" = "
onclick="Operation('=')">
</td>
</tr>
</table>
</table>
</B>
</form>
</center>
<font face="Verdana, Arial, Helvetica"
size=2>
<script language="JavaScript"
>
var funKeyPad = document.keys;
var Accumulate = 0;
var NewNum = false;
var PendingOp = "";
function number_press (Num) {
if (NewNum) {
funKeyPad.readText.value = Num;
NewNum = false;
}
else {
if (funKeyPad.readText.value ==
"0")
funKeyPad.readText.value = Num;
else
funKeyPad.readText.value += Num;
}
}
function Operation (Op) {
var Readout = funKeyPad.readText.value;
if (NewNum && PendingOp !=
"=");
else
{
NewNum = true;
if ( '+'
== PendingOp )
Accumulate += parseFloat(Readout);
else if ( '-'
== PendingOp )
Accumulate -= parseFloat(Readout);
else if ( '/'
== PendingOp )
Accumulate /= parseFloat(Readout);
else if ( '*'
== PendingOp )
Accumulate *= parseFloat(Readout);
else
Accumulate = parseFloat(Readout);
funKeyPad.readText.value = Accumulate;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = funKeyPad.readText.value;
if (NewNum) {
curReadOut = "0.";
NewNum = false;
}
else
{
if (curReadOut.indexOf
(".") == -1
)
curReadOut +=
".";
}
funKeyPad.readText.value = curReadOut;
}
function ClearEntry () {
funKeyPad.readText.value = "0";
NewNum = true;
}
function Clear () {
Accumulate = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
funKeyPad.readText.value = parseFloat(funKeyPad.readText.value) * -
1;
}
function Percent () {
funKeyPad.readText.value = (parseFloat(funKeyPad.readText.value) /
100) * parseFloat(Accumulate);
}
</script>
</body>
</html>
OutPut: