Results 1 to 4 of 4
  1. #1
    Kevin BSU is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Help with simple control statement

    I'm trying to write a script that prints out the total numbers of 1 to 100 using "while" statements, i.e., add all numbers from 1 to 100.

    This is what I have but it doesn't work. I'm new to this stuff...any help?

    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head>
    <title>Total of 1 to 100</title>
    <script type = "text/javascript">
    addNumbers() {
    var count = 0;
    var result = 0;

    while (count < 101) {
    result += count;
    count++;
    }
    window.alert("The sum of the numbers from one to one hundred is: " + result);
    }
    </script>
    </head>
    <body>
    <p>Click Refresh (or Reload) to run the script again</p>
    </body>
    </html>

  2. #2
    Kevin BSU is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Never mind, I got it. I was way off! Here's the correct script for anyone who care:

    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head><title>Sum the Integers from 1 to 100</title>
    <script type = "text/javascript">

    var sum; // stores the total
    var x; //counter control variable

    x = 1;
    sum = 0;

    while ( x <= 100 )
    {
    sum += x;
    ++x;
    } // end while
    document.writeln( "The sum is: " + sum )
    </script>
    </head><body></body>
    </html>

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This has nothing to do with Java and all to do with JavaScript, an unrelated language.

    Locking in a minute or three.

  4. #4
    Kevin BSU is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. a simple if statement
    By senca in forum New To Java
    Replies: 16
    Last Post: 02-25-2010, 10:17 PM
  2. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 10:56 PM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 03:55 AM
  4. control app width based on certain control
    By thebillybobjr in forum SWT / JFace
    Replies: 0
    Last Post: 05-15-2008, 05:52 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Tags for this Thread

Posting Permissions

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