Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2006, 4:32 AM   #1
Uday
Programmer
 
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0 Uday is on a distinguished road
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);
}
Uday is offline   Reply With Quote
Old Mar 7th, 2006, 5:00 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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).
The Dark is offline   Reply With Quote
Old Mar 7th, 2006, 5:20 AM   #3
Uday
Programmer
 
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0 Uday is on a distinguished road
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.
Uday is offline   Reply With Quote
Old Mar 7th, 2006, 7:52 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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);
}
The Dark is offline   Reply With Quote
Old Mar 7th, 2006, 8:57 AM   #5
Uday
Programmer
 
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0 Uday is on a distinguished road
It works. Thanks.
Uday is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:38 PM.

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