View Single Post
  #5 (permalink)  
Old Oct 9th, 2005, 14:33
s_mazhar s_mazhar is offline
Junior Member
Join Date: Sep 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
urgent help req...

Plz Check this code n tell me is it rite approach...

<html>
<head>
<title> Java Script Practice </title>
<script language="JavaScript">

var savestuff="";

var stack="";
function adddigit(kaller){
if(kaller=='+')
{
op=kaller;

document.My.first.value="";
stack=eval(stack+'+'+savestuff);

document.My.first.value=stack;
savestuff=0;
}

else if(kaller=='*')
{
op=kaller;
if(stack=="")
{
stack=1;
document.My.first.value="";
stack=eval(stack+'*'+savestuff);

document.My.first.value=stack;

savestuff=0;
}
else
{

document.My.first.value="";
stack=eval(stack+'*'+savestuff);

document.My.first.value=stack;

savestuff=0;

}
}

else if(kaller=="=")


{
dumath(stack,savestuff,op);

}

else

{
savestuff=savestuff+kaller;
document.My.first.value=savestuff;

}


return

}


function dumath(f,sec,opt)
{

if(opt=="+")
{
anser=eval(f+'+'+sec);
document.My.first.value=anser;
}

else if(opt=="*")
{
anser=eval(f+'*'+sec);
document.My.first.value=anser;
}

return;

}



</script>
</head>
<body>

<form name="My">
<input type="text" name="first">

<table border="2" align=Center bgcolor="Blue">
<tr>
<td>

<input type="button" size="40" value="1" name="b1" onclick="adddigit('1')">
<input type="button" size="40" value="2" name="b2" onclick="adddigit('2')">
<input type="button" size="40" value="3" name="b3" onclick="adddigit('3')">

</td>
<tr>
<td>

<input type="button" size="40" value="4" name="b4" onclick="adddigit('4')">
<input type="button" size="40" value="5" onclick="adddigit('5')">
<input type="button" size="40" value="6" onclick="adddigit('6')">
</td>

</tr>


<tr>
<td>

<input type="button" size="40" value="7" onclick="adddigit('7')">
<input type="button" size="40" value="8" onclick="adddigit('8')">
<input type="button" size="40" value="9" onclick="adddigit('9')">
</td>
</tr>


<tr>
<td>
<input type="button" size="40" value="+" onclick="adddigit(value)">
<input type="button" size="40" value="=" onclick="adddigit(value)">
<input type="button" size="40" value="*" onclick="adddigit(value)">
</tr>
</tr>


</form>
</body>
</html>

m having some problems in it...when i entered sec. no it displays a leading zero wid it which is the value of var.."stack"..bt if I don't initialize it then eval function perform string concatination..

Its not a complete calculater only performing add n mul operations till noww
reply sooon
Reply With Quote