Results 1 to 3 of 3
  1. #1
    WDM
    WDM is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Help to change While Loop to a For Loop

    Hi, I'm new to Javascript and I was given an assignment to write a For Loop using this while loop as a base.

    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function start()
    {
    var again = true;
    while(again)
    {
    var number = window.prompt('Please enter a number');
    document.getElementById('table').innerHTML = generateTableContents(parseInt(number));
    again = window.confirm('Again?');
    }

    }

    function generateTableContents(number)
    {
    var end = number + 9;
    var currentNum = number;
    var innerTable = '';
    while(currentNum <= end)
    {
    innerTable = innerTable + "<tr> <td>" + currentNum+"</td><td>"+ currentNum * currentNum + "</td></tr>\n";
    currentNum++;
    }
    console.log(innerTable);
    return innerTable;

    }

    </script>
    </head>

    <title>While Loop</title>

    <body onload="start();">
    <form name = "formpage">
    <table id="table" >
    </table>
    </form>
    <button id="again" onclick="start();">Try again</button>
    </body>
    </html>

    Would anyone be able to help me? Thanks!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,597
    Rep Power
    5

    Default Re: Help to change While Loop to a For Loop

    Wrong forum. This is for Java. JavaScript is something different.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Help to change While Loop to a For Loop

    Behold the power of Google and w3schools!

    Regards.

Similar Threads

  1. change do-while loop to while
    By Souverain in forum New To Java
    Replies: 4
    Last Post: 12-29-2012, 10:51 PM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. change text colors through nested loop
    By Bader in forum Java Applets
    Replies: 1
    Last Post: 05-09-2010, 02:24 AM
  4. Replies: 18
    Last Post: 11-29-2009, 11:58 PM
  5. Change my for loop
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 12-12-2007, 11:00 AM

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
  •