Created
September 6, 2019 04:27
-
-
Save masautt/488736f2ec7d4cc2d68e258da54be37a to your computer and use it in GitHub Desktop.
How to reverse a sentence in JavaScript?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseWords(str) { | |
var result = ""; | |
var wordArray = str.split(" "); | |
for(var i = wordArray.length - 1; i >= 0; i--) { | |
result += wordArray[i] + " "; | |
} | |
return result.trim(); | |
} | |
console.log(reverseWords("Marek E. Sautter")); // --> "Sautter E. Marek" | |
// https://www.thepolyglotdeveloper.com/2015/02/reverse-words-string-using-javascript/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment