|
-
Feb 21st, 2001, 06:21 PM
#1
Thread Starter
Addicted Member
Adding numbers in JScript Problem
I am VERY VERY NEW to JScript, I am wondering what is wrong with this code to make a 2 number adder:
var a = prompt("First Number:","");
var b = prompt("Second Number:","");
alert (a + " + " + b + " = " + (a + b))
It does everything but it just combines then number, like 2+6=26
HELLP!
Last edited by Frankie902; Feb 22nd, 2001 at 04:01 PM.
-
Feb 21st, 2001, 08:32 PM
#2
Hi,
Use the ParseInt to convert the string to an integer and then use the eval method.
Code:
var a = parseInt(prompt("First Number:",""));
var b = parseInt(prompt("Second Number:",""));
alert (a + " + " + b + " = " + eval(a + b));
[VBF RSS Feed]
There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.
If I have been helpful, Please Rate my Post. Thanks.
This post was powered by : 
-
Feb 22nd, 2001, 03:43 AM
#3
Frenzied Member
...or just use eval()
Code:
var a = eval(prompt("First Number:",""));
var b = eval(prompt("Second Number:",""));
alert (a + " + " + b + " = " + (a + b))
-
Feb 23rd, 2001, 07:48 AM
#4
Junior Member
Superb ! carrying on from this
if i have 3 text boxes - left,center and right
what would the code be to add left and center values to present the answer in right ?
Any help is appreciated
-
Feb 26th, 2001, 06:48 AM
#5
Frenzied Member
visordown
This works in IE
I haven't tried it in NS
Code:
<HTML>
<HEAD>
<script language=JavaScript>
function addThem()
{
document.frm1.txt3.value=eval(document.frm1.txt1.value)+ eval(document.frm1.txt2.value)
}
</script>
</HEAD>
<BODY>
<form name=frm1>
<input name=txt1 size=4 onKeyup="addThem()">
<input name=txt2 size=4 onKeyup="addThem()">
<input name=txt3 size=4>
</form>
</BODY>
</HTML>
-
Feb 26th, 2001, 07:00 AM
#6
Junior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|