Reverse the words : Words « String « JavaScript Tutorial






<HTML>
<HEAD>
<SCRIPT>
function revWords(str){
   words = str.split(" ");
   j = words.length - 1;
   backWords = new Array(); 
   for (i=0 ; i < words.length ; i++){
      backWords[j] = words[i];
      j--
   }         
   document.theForm.results.value = backWords.join(" ");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm">
Enter a text string:
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<INPUT type=button value="Reverse Words" onClick="revWords(document.theForm.inStr.value)";>
<INPUT type=button name="theButton" value="Clear Results" onClick='document.theForm.results.value=""';>
Results<br>
<TEXTAREA name=results rows=5 cols=90>
</TEXTAREA>

</FORM>  
</BODY>
</HTML>








6.38.Words
6.38.1.Capitalize the words
6.38.2.Count the words
6.38.3.Reverse the words