![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
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);
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 822
Rep Power: 4
![]() |
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);
} |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
It works. Thanks.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|