Results 1 to 4 of 4

Thread: 5 numbers added with a FOR loop

  1. #1
    Join Date
    Apr 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 5 numbers added with a FOR loop

    I'm very new to JavaScript and am faced with the problem of making a function that declares an array of five numbers. Initialize the five values from textboxes in a form. Using a for loop, sum the elements of the array and display the result on the web page when the submit button is clicked. that will take.

    here is what I have ... I hope it isn't way off:

    <script type="text/javascript">
    <!-- HIDE FROM INCOMPATIBLE BROWSERS
    var n = new Array();
    n[0] = parseInt(document.form[0].num1.value);
    n[1] = parseInt(document.form[0].num2.value);
    n[2] = parseInt(document.form[0].num3.value);
    n[3] = parseInt(document.form[0].num4.value);
    n[4] = parseInt(document.form[0].num5.value);
    function numbers() {
    for (var count = 0; count < 4; ++count) {
    var answer = answer + n[count];
    document.write(answer);
    }
    }

    //STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>


    <form>
    <p>Insert a number in each text space and click "submit."</p>
    <input type="text" name="num1">
    <input type="text" name="num2">
    <input type="text" name="num3">
    <input type="text" name="num4">
    <input type="text" name="num5">
    <input type="button" value="Submit" onclick="numbers();" />
    </form>


    any help with this?

    thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using parseInt() is probably safer but, multiplying by one seems a better choice if it works, and it does in Opera8.54 FF1.5.2 and IE6. Hiding is useless. You had a number of minor errors and using document.write will, of course, overwrite your page so I made a readonly answer field:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function numbers() {
    var answer = 0,
    n = new Array();
    n[0] = 1*document.forms[0].num1.value;
    n[1] = 1*document.forms[0].num2.value;
    n[2] = 1*document.forms[0].num3.value;
    n[3] = 1*document.forms[0].num4.value;
    n[4] = 1*document.forms[0].num5.value;
    for (var count = 0; count < 5; count++)
    answer += n[count];
    document.forms[0].ans.value=answer;
    }
    </script>
    </head>
    <body>
    <form>
    <p>Insert a number in each text space and click "submit."</p>
    <input type="text" name="num1">
    <input type="text" name="num2">
    <input type="text" name="num3">
    <input type="text" name="num4">
    <input type="text" name="num5">
    <input type="button" value="Submit" onclick="numbers();" /><br>
    Answer: <input type="text" readonly name="ans">
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The comment delimiters are unnecessary. So's the whole script block, actually. And why have you used an XHTML-style input tag for the button, but HTML-style for everything else? Also, the method and action attributes of <form> are required.
    Code:
    <form action="" method="post">
    <p>Insert a number in each text space and click "submit."</p>
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="text">
    <p id="total"></p>
    <input type="button" value="Submit" onclick="
      var total = 0,
        e = document.forms[0].elements,
        tmp = 0;
      for(var i=0;i<e.length;i++) if(!isNaN(tmp = parseInt(e[i].value))) total += tmp;
      document.getElementById('total').innerHTML = total;
    ">
    </form>
    /EDIT: John seems to have beaten me to it, but my code is shorter, so I'll leave it here so you can choose.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Apr 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you both for your help. As for yall's questions, that's just how this low-end JavaScript book teaches it.

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
  •