Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to programming and I've read and watched several resources about for, while loops, but everytime I write the while loop to reverse the output from the for loop, the code doesn't run properly. I'm sort of just guessing at this point.

Does anyone know of a good article/video to better understand reversing the output from user input by using a while loop?

I commented out the while loop I was working on.

What I have tried:

HTML
<html>
 
<body style="text-align:center;">
 
    <h1>Enter Your Five Favorite Cities</h1>
    <form class="" action="index.html">
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
        <br>
        <input type="text" name="favoriteCities[]" value="" /><br>
    <br>
        <button type="button" name="button" onclick="favoriteCities()">Submit</button>
  
    </form>
 
    <h2>Results</h2>
 
    <p id="output"></p>
    
    <script type="text/javascript">
        var r = "";
        function favoriteCities() {
            var input = document.getElementsByName('favoriteCities[]');
 
            for (var i = 0; i < input.length; i++) {
                var a = input[i]; var uniqueNumber=i+1;
                r = r + "City #" + uniqueNumber + " is " + a.value + ("<br>");
            }
            
            document.getElementById("output").innerHTML = r;
            
            //while (4 = input.length) {
            //print (output)
            //}
        }
        
    </script>
    
</body>
 
</html>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900