Reversing output of user input by using while loop...

Joined
Aug 31, 2022
Messages
8
Reaction score
0
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.

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>
 
Joined
Sep 3, 2022
Messages
1
Reaction score
0
Hello there!

I do not know what you mean by "the while loop to reverse the output from the for loop". If what you want is to reverse the strings that the user did input, you can try something like this:

HTML:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- displays site properly based on user's device -->
        <!--<link rel="stylesheet" href="styles.css" />-->
        <!--<script src="script.js" defer></script>-->
        <title></title>
    </head>

    <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 reverseString(strParam)
        {
            let str = "";
            for(let i = 0; i < strParam.length; i++)
            {
                str = str + strParam[strParam.length -1 -i];
            }
            
            return str;
        }

        function favoriteCities() {
            var input = document.getElementsByName('favoriteCities[]');
 
            for (var i = 0; i < input.length; i++)
            {
                var a = reverseString(input[i].value);
                var uniqueNumber=i+1;
                r = r + "City #" + uniqueNumber + " is " + a + ("<br>");
            }
            
            document.getElementById("output").innerHTML = r;
            
        }
        
    </script>
    
</body>

</html>
 
Joined
Mar 11, 2022
Messages
226
Reaction score
32
I don't know exactly what you are trying to do there but a while loop works like this
Code:
var array=new Array('Apple','Peach','Pear','Berry');
var num=array.length;
while(num>0){
 console.log(array[num-1]);
num--;
}

Same thing with a for loop
Code:
var array=new Array('Apple','Peach','Pear','Berry');
var num=array.length;
for(i=num; i>0; i--){
 console.log(array[i]);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top