Using a Do/While Loop to Reverse a Text String : While « Language Basics « JavaScript DHTML






Using a Do/While Loop to Reverse a Text String

  
<HTML>
<BODY>
<H1>
<SCRIPT> 
      var newString = ""; 
      var theString = "string"; 
      var counter = theString.length; 
      do 
      { 
        newString += theString.substring(counter-1, counter); 
        counter--; 
      } 
      while (counter > 0 ); 
      document.write(theString + " reversed is " + newString + "!"); 
   </SCRIPT>
</H1>
</BODY>
</HTML>


           
         
    
  








Related examples in the same category

1. A While Loop That Decrements from 10 to 1
2.While loop
3.Do while loop
4.Using the while Loop in JavaScript
5.The do..while Statement Ensures at Least One Iteration
6.The while Statement
7.Loop in while
8.While loop test
9.Check the loop counter for while loop
10.Do ... while loop and output calculation result