Results 1 to 4 of 4

Thread: Adding User-Given Numbers

  1. #1
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Adding User-Given Numbers

    For a class, I'm trying to create a script that prompts the user for 3 numbers, adds them together, and displays the sum in an alert box. These are the requirements from my teacher:

    Using Code Example 4.3 from Lesson Four, write a similar JavaScript that collects three integers from the user, then adds these numbers together (via a function) and returns the results back to the program. These results should then be displayed, via an alert() method, to the user.

    NOTE: Your function will not contain any if-statements, but instead will have a line something like "return x+y+z", which means the values of x, y, and z are to be added together and then returned back as the function value.


    Here is code example 4.3:

    function compareTwo(x, y)
    {
    if (x==y)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    // get two integers from the user
    var a = prompt("Enter one integer:","");
    var b = prompt("Enter another integer:","");

    // pass these values to compareXY, defined above.
    // if they are the same, the function will return "true",
    // otherwise it will return "false".

    if (compareTwo(a,b))
    {
    alert("Your numbers were identical!");
    }
    else
    {
    alert("Your numbers were different!");
    }


    And below is what I have figured out so far. This code will give "ans" as a string of numbers, instead of the sum of the numbers.

    function addThree(x,y,z) {
    return x+y+z;
    }

    var a = prompt("Enter one integer:","");
    var b = prompt("Enter a second integer:","");
    var c = prompt("Enter a third integer:","");

    var ans = addThree(a,b,c);

    alert("The sum of your numbers is " + ans);


    I can tell something is missing, but I just can't figure out what to add! If somebody could help me, that would be awesome, thanks! X3 ♥
    Last edited by Aeche; 04-08-2009 at 02:41 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    In the function, you have to add numbers, not strings. Turn them into ints using the parseInt method:
    Code:
    return parseInt(x)+parseInt(y)+parseInt(z);
    That should do it.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    Aeche (04-08-2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    It did work!!! Thank you so much, you're awesome! *glomp*

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"


    If you need an explanation:

    The only reason example 4.3 works is because its testing string to string, your adding strings together.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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