Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: While Loops

  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    While Loops

    I have a code that I can't seem to get to work and it's been driving me nuts for hours.

    I have to use a While Loop to prompt the user for the name of their favorite team. Use a while loop to display a cheer three times. If the user entered "Braves", the program would output:
    Go Braves!
    Go Braves!
    Go Braves!

    the example code the book gives me is

    while ( index < 3) {
    document.write("Beetlejuice ");
    index = index + 1;

    }

    The prompt works fine, but I can't get it to display properly. Any help would aid in me not smashing my head on the keyboard anymore.

    Thank you!
    Last edited by galaxiesanddust; 10-10-2012 at 10:38 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Did you set index to equal 1 before the loop?

    Don't forget to reset to 0 after the loop.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I did set index to 0 before the loop. This is the code I have so far. I'm fairly new to all this.

    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    // Program name: cheer.html
    // Purpose: 
    // Author: 
    // Date last modified: 
    
    // Declare Variables and Constants
    var teamName;
    var index = 0;
    
    // Prompt user for the name of their favorite team
    teamName = prompt("Enter your favorite team");
    teamName = parseFloat(teamName);
    
    // Display
    index = 0;
    while ( index < 3); {
          document.write( "teamName" );
          index = index + 1;
    
    }
    
    </script>
    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    parseFloat is for integers. What are you trying to do to the string?

    Also.. in this line, remove the quotes.
    document.write( "teamName" );
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    New Coder
    Join Date
    Aug 2010
    Posts
    53
    Thanks
    6
    Thanked 1 Time in 1 Post
    Try using a for loop:
    PHP Code:
    $a "Go "
    $a .= "[Team]!"

    $i $a;
    for (
    $n=0$n<3$n++) {
        
    $output "<br/>".$i++ . "\n";
        
        echo 
    $output;

    Output dispaly:
    PHP Code:
    Go [Team]! 
    Go [Team]! 
    Go [Team]! 
    Last edited by Wuteverx1972; 10-10-2012 at 09:01 PM. Reason: oops nvm... you meant js.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    OP isn't using PHP, AFAIK.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #7
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I removed the quotes and changed parseFloat. I'm just trying to have whatever the user enters in prompt at the beginning to appear three times. Is there a way I need to close the loop or something that I am overlooking? Right now the page just acts like it's continuously loading.

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    There's also a semicolon where there shouldn't be. (while ( index < 3); {)

    Try this:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    
    // Program name: cheer.html
    // Purpose: 
    // Author: 
    // Date last modified: 
    
    // Declare Variables and Constants
    var teamName;
    var index = 0;
    
    // Prompt user for the name of their favorite team
    teamName = prompt("Enter your favorite team");
    
    // Display
    for ( index=0; index < 3; index++) {
          document.writeln( teamName + " " );
    
    }
    
    </script>
    </body>
    </html>
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    galaxiesanddust (10-10-2012)

  • #9
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That worked. Thank you so much for the help!

  • #10
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    np.. glad I could help.. please edit your original post and change the header to RESOLVED.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    The guy asked for a while loop.

    Code:
    <html>
    <head>
    </head>
    
    <body>
    <script type="text/javascript">
    
    // Program name: cheer.html
    // Purpose: 
    // Author: 
    // Date last modified: 
    
    // Declare Variables and Constants
    var teamName;
    var index = 0;
    var Jmessage = "";
    var Hmessage = "";
    
    
    // Prompt user for the name of their favorite team
    teamName = prompt("Enter your favorite team", "");
    
    // Display
    while ( index < 3) {
    Jmessage +=  teamName + "\n";  // Note \n is newline in Javascript
    Hmessage += teamName + "<br>";  // But <br> is newline in HTML
    index ++;
    }
    
    alert (Jmessage);
    document.write(Hmessage);  // document.write() creates a new page
    
    </script>
    </body>
    </html>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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