...

View Full Version : pls help me debug my js calculator



viny0013
07-08-2011, 08:26 PM
I've got this code below, it seems ok, but sometimes, the multiplication does not work..i don't know why... :( pls help me..thanks :(

btw, this is my 1st javascript "application", so, im really new into this
:D

thanks in advance for any help :D :thumbsup:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My First JavaScript Application</title>

<style type="text/css">
td
{
text-align:center
}
table
{
background:#660033
}
body
{
background-color:#666;
text-wrap:supress;

}
h1
{
color:#FFFFFF;
}

.ex
{
background-color:#99F;
color:#660000;
font:Verdana, Geneva, sans-serif;
font-size:20px;
text-align:right;

}
.ec
{
background-color:#CCC;
color:#660033;
font:Arial, Helvetica, sans-serif;
font-size:18px;
}
</style>


<script type="text/javascript">
var num1 = 0;
var ope = 0;
var num2 = 0;
var ek = 0

function addOnScr1()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+1;
document.getElementById("nums").value = txt;

}

function addOnScr2()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+2;
document.getElementById("nums").value = txt;
}

function addOnScr3()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+3;
document.getElementById("nums").value = txt;
}

function addOnScr4()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+4;
document.getElementById("nums").value = txt;
}

function addOnScr5()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+5;
document.getElementById("nums").value = txt;
}

function addOnScr6()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+6;
document.getElementById("nums").value = txt;
}
function addOnScr7()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+7;
document.getElementById("nums").value = txt;
}

function addOnScr8()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+8;
document.getElementById("nums").value = txt;
}
function addOnScr9()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+9;
document.getElementById("nums").value = txt;
}

function addOnScr0()
{
document.getElementById("neg").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}

var txt = document.getElementById("nums").value;
txt = txt+0;
document.getElementById("nums").value = txt;
}

function addOnScrp()
{
document.getElementById("neg").disabled=true;
document.getElementById("point").disabled=true;
if((ek!=0))
{
document.getElementById("nums").value = "";
ek = 0;
}
var txt = document.getElementById("nums").value;
txt = txt+".";
document.getElementById("nums").value = txt;
}

function addOnScrn()
{
var txt = document.getElementById("nums").value;
txt = txt+"-";
document.getElementById("nums").value = txt;
}

function add()
{
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;
ek=1;
if(num1==0)
{
num1 = parseFloat(document.getElementById("nums").value);
}
else
{
ent();
}
ope = 1;

}

function diff()
{
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;

ek=1;
if(num1==0)
{
num1 = parseFloat(document.getElementById("nums").value);
}
else
{
ent();
}
ope = 2;
}

function prod()
{
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;

ek=1;
if(num1==0)
{
num1 = parseFloat(document.getElementById("nums").value);
}
else
{
ent();
}
ope = 3;
}

function quo()
{
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;

ek=1;
if(num1==0)
{
num1 = parseFloat(document.getElementById("nums").value);
}
else
{
ent();
}
ope = 4;
}

function clr()
{
num1 = 0;
num2 = 0;
ope = 0;
document.getElementById("nums").value = "";
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;
ek = 0;
}

function ent()
{
ek = 1;
var ans;
num2 = parseFloat(document.getElementById("nums").value);
switch(ope)
{
case 1:
ans = num1 + num2;
document.getElementById("nums").value = ans;
num1 = ans;
break;
case 2:
ans = num1 - num2;
document.getElementById("nums").value = ans;
num1 = ans;
break;
case 3:
ans = num1 * num2;
document.getElementById("nums").value = ans;
num1 = ans;
break;
case 4:
ans = num1 / num2;
document.getElementById("nums").value = ans;
num1 = ans;
break;
default:
document.getElementById("nums").value = "???";
}
}

function enterz()
{
document.getElementById("neg").disabled=false;
document.getElementById("point").disabled=false;

ek = 1;
var ans;
num2 = parseFloat(document.getElementById("nums").value);
switch(ope)
{
case 1:
ans = num1 + num2;
document.getElementById("nums").value = ans;
num1 = 0;
break;
case 2:
ans = num1 - num2;
document.getElementById("nums").value = ans;
num1 = 0;
break;
case 3:
ans = num1 * num2;
document.getElementById("nums").value = ans;
num1 = 0;
break;
case 4:
ans = num1 / num2;
document.getElementById("nums").value = ans;
num1 = 0;
break;
default:
document.getElementById("nums").value = "???";
}
}



</script>
</head>
<body><center>
<form><h1>
Calculator</h1>
<table border="0">
<tr><th colspan="4"><input type="text" class = "ex" id = "nums" name = "nums" /></th></tr>
<tr><td><button type="button" class = "ec" onclick="addOnScr1()">1</button></td>
<td><button type="button" class = "ec" onclick="addOnScr2()">2</button></td>
<td><button type="button" class = "ec" onclick="addOnScr3()">3</button></td>
<td><button type="button" class = "ec" onclick="add()">+</button></td>
</tr>

<tr><td><button type="button" class = "ec" onclick="addOnScr4()">4</button></td>
<td><button type="button" class = "ec" onclick="addOnScr5()">5</button></td>
<td><button type="button" class = "ec" onclick="addOnScr6()">6</button></td>
<td><button type="button" class = "ec" onclick="diff()">--</button></td></tr>

<tr><td><button type="button" class = "ec" onclick="addOnScr7()">7</button></td>
<td><button type="button" class = "ec" onclick="addOnScr8()">8</button></td>
<td><button type="button" class = "ec" onclick="addOnScr9()">9</button></td>
<td><button type="button" class = "ec" onclick="prod()">*</button></td></tr>

<tr><td><button type="button" class = "ec" id="neg" onclick="addOnScrn()">-</button></td>
<td><button type="button" class = "ec" onclick="addOnScr0()">0</button></td>
<td><button type="button" class = "ec" id="point" onclick="addOnScrp()">.</button></td>
<td><button type="button" class = "ec" onclick="quo()">/</button></td></tr>

<tr><td></td> <td><button type="button" class = "ec" onclick="enterz()">=</button></td>
<td><button type="button" class = "ec" onclick="clr()">C</button></td><td> </td></tr>

</form>

</body>
</html>

Old Pedant
07-08-2011, 10:05 PM
Well, for starters, why do you have addOnScr0() through addOnScr9() when you could just have addOnScr(0) through addOnScr(9) [that is, use the same function for all digit keys].

And your "neg" key logic is tortured. Among other things, you don't allow a negative number to be flipped to positive.

I would rewrite it as

function addOnScrNegate()
{
var txt = document.getElementById("nums").value;
if( txt.charAt(0) == "-" ) txt = txt.substring(1);
else txt = "-" + txt;
document.getElementById("nums").value = txt;
}

and then get rid of disabling the "neg" key all the time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum