Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Calculator (http://www.programmingforums.org/showthread.php?t=8749)

Uday Mar 7th, 2006 4:32 AM

Calculator
 
:

<html>
<head>
<title>
Calculator
</title>
<body bgcolor="pink">
<script language="javascript">
function add()
{
var i=0;
i=document.one.first.value + document.one.second.value;
document.write(i);
}

function sub()
{
var i=0;
i=document.one.first.value - document.one.second.value;
document.write(i);
}

function mul()
{
var i=0;
i=document.one.first.value * document.one.second.value;
document.write(i);
}

function div()
{
var i=0;
i=document.one.first.value/document.one.second.value;
document.write(i);
}

</script>
<form name="one">
Enter numbers
<input type="text" name="first" value="">
<input type="text" name="second" value="">
<p></p><input type="Submit" name="ADD" value="ADDITION" onClick="add()">
<input type="Submit" name="SUB" value="SUBSTRACT" onClick="sub()">
<input type="Submit" name="MUL" value="MULTIPLY" onClick="mul()">
<input type="Submit" name="DIV" value="DIVIDE" onClick="div()">
</form>
</body>
</html>


The code is working except the addition part. Any solution?

i.e
:

function add()
{
var i=0;
i=document.one.first.value + document.one.second.value;
document.write(i);
}


The Dark Mar 7th, 2006 5:00 AM

You probably need to convert the values to numbers before adding them together, try something like:
:

function add()
{
var i=0;
i=document.one.first.value + 0 + document.one.second.value;
document.write(i);
}

The 0 in there should make the whole expression into a numeric one (I think).

Uday Mar 7th, 2006 5:20 AM

when 4 and 4 is given it earlier used to show 44

Now it shows 404

The other things like substraction, multiplication and division is working but only additon is not.

The Dark Mar 7th, 2006 7:52 AM

Oops, sorry about that, must have got my languages mixed up
This one works (I tried it first!)
:

function add()
{
var i=0;
i=parseInt(document.one.first.value) + parseInt(document.one.second.value);
document.write(i);
}


Uday Mar 7th, 2006 8:57 AM

It works. Thanks.


All times are GMT -5. The time now is 4:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC