Results 1 to 4 of 4

Thread: Adding

  1. #1
    Join Date
    Nov 2002


    Hey, I have to do this for Homework, and I'm doing something wrong. I was hoping someone could help me out.

    I think its obvious what I'm trying to do. I have to prompt the user for three numbers, after that they can click on a button the will call the function. The function is then suppose to add these numbers up. However, it's not working. I believe, its looking at them like they were just a reg string, but i thought it only did that when u used "". I'm not sure anymore. If anyone could give me a hint or tell me what I'm doing wrong and why, it would be great. Thanks a lot in advance.

    Here is the code: (If comment tags are needed I'll add them)



    <TITLE>The Adding Function, JavaScript2</TITLE>

    <SCRIPT LANGUAGE="JavaScript">

    function AddMe(A,B,C) {

    var NumOne = A;
    var NumTwo = B;
    var NumThree= C;
    var Sum = "";

    Sum =(NumOne + NumTwo + NumThree); //I believe the mistake is this line






    <SCRIPT LANGUAGE="JavaScript">

    A = prompt("Enter a Number","");
    B = prompt("Enter another Number","");
    C = prompt("Enter yet, another Number","");


    <INPUT type="button" value="Add those Numbers!"




  2. #2
    Join Date
    Nov 2002
    Prompts (and forms) retrun text strings. The "+" operator is duplicative (has two uses). It concatenates strings, or adds numbers. You have to convert the prompt values into numbers. There are three functions you could use for that - parseInt(), parseFloat(), and Number(). You can also fool the interpreter into converting them for you by multiplying one (or all) the values by 1.

    var NumOne = Number(A);
    var NumTwo = Number(B);
    var NumThree= Number(C);
    var Sum = NumOne + NumTwo + NumThree;

  3. #3
    Join Date
    Nov 2002
    Thankyou Mr. Davis. I mean it..thanks a lot especially for explaining it and just not fixing it for me. I'll have to look in my book about those three functions, but you pointed me in the right direction, thanks a bill. BTW your intials wouldn't be DVD? my intructors name is davis.

  4. #4
    Join Date
    Nov 2002
    O okay I got it, doing the number() thing is saying "Hey don't listen to the other strings, no matter what A, B, and C is going to be a number value, not a text string"...The light went on once I put it in code... Your awesome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles