Javascript String reverseWords()
String.prototype.reverseWords = function() { var arr = this.split(" "); var s = ""; while (arr.length>0) { var element = arr.pop(); s += element + " "; } return s;/*from w w w. ja v a 2 s .com*/ } console.log("this is a test ajsd".reverseString());
/**/*from w w w. j a va2 s . c o m*/ * Reverse the order of words in a sentence. Complete this function. */ var sentence = "The quick brown fox."; function reverseWords(str) { } reverseWords(sentence); // should return // "fox. brown quick The" // BONUS // Write the function so that I can call the following and it will return the same result. "The quick brown fox.".reverseWords(); // ANSWER function reverseWordsSolved(str) { return str.split(' ').reverse().join(' '); } // BONUS ANSWER String.prototype.reverseWords = function() { return this.split(' ').reverse().join(' '); };