Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 6, 2019 04:27
Show Gist options
  • Save masautt/488736f2ec7d4cc2d68e258da54be37a to your computer and use it in GitHub Desktop.
Save masautt/488736f2ec7d4cc2d68e258da54be37a to your computer and use it in GitHub Desktop.
How to reverse a sentence in JavaScript?
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