Skip to content

Instantly share code, notes, and snippets.

@ChousinRahit
Last active September 3, 2023 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChousinRahit/d027fff3e1ab569c9218a0eabe826c43 to your computer and use it in GitHub Desktop.
Save ChousinRahit/d027fff3e1ab569c9218a0eabe826c43 to your computer and use it in GitHub Desktop.
// Reverse Words in a String
// Given an input string, reverse the string word by word, the first word will be the last, and so on.
// Examples
// reverseWords(" the sky is blue") ➞ "blue is sky the"
// reverseWords("hello world! ") ➞ "world! hello"
// reverseWords("a good example") ➞ "example good a"
// Pseudocode
// Write a function "reverseWords" that takes one string argument
// split the string with empty spaces
// reverse the array and join them
// resturn result
function reverseWords(str) {
return str.split(' ').reverse().join(' ');
}
console.log(reverseWords(' the sky is blue'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment