× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Reverse a phrase using javascript

Silvio Moreto
Feb 10, 2016
<p><span style="color:rgb(95, 99, 102)">To reverse a string using javascript is pretty simple. Le'ts create the function:</span></p> <p> </p> <pre><code class="language-javascript">var phrase = "This is fun, hopefully."; function wordsReverser(words) { var return_words = '', split_words, p_check = ['.', ',', ';'];; // this wil split the words by spaces split_words = words.split(' '); for(var word in split_words) { var r_word = split_words[word].split('').reverse().join(''); for(p in p_check) { if(p_check[p] == r_word.charAt(0)) { first_char = r_word.charAt(0); r_word = r_word.substr(1); r_word += first_char; } } return_words += r_word + ' '; } return return_words; } wordsReverser("This is fun, hopefully.");</code></pre> <p>This is a simple snippet of how you can solve this problem. </p>
comments powered by Disqus