Results 1 to 6 of 6

Thread: Add numbers in JScript

  1. #1

    Thread Starter
    Addicted Member Frankie902's Avatar
    Join Date
    Feb 2001
    Location
    Lindenwold, NJ, USA
    Posts
    217

    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.

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    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 :

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    ...or just use eval()
    Code:
    var a = eval(prompt("First Number:",""));
    var b = eval(prompt("Second Number:",""));
    alert (a + " + " + b + " = " + (a + b))
    Mark
    -------------------

  4. #4
    Junior Member
    Join Date
    Aug 2000
    Location
    Rotterdam
    Posts
    19
    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

  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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>
    Mark
    -------------------

  6. #6
    Junior Member
    Join Date
    Aug 2000
    Location
    Rotterdam
    Posts
    19
    thankyou very much u r far too clever for your own good


    cheers a lot pal !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width