Skip to content

Instantly share code, notes, and snippets.

@bhaireshm
Last active September 27, 2022 16:45
Show Gist options
  • Save bhaireshm/c8a319b06b5fb69d1f50fe758f380311 to your computer and use it in GitHub Desktop.
Save bhaireshm/c8a319b06b5fb69d1f50fe758f380311 to your computer and use it in GitHub Desktop.
Reverse a word, sentence or reverse each words in a sentence.
// @param str: string without space
function reverseStr(str){
return str.split('').reverse().join('');
}
// @param sentence: pass a sentence.
function reverseEachWordInSentence(sentence){
return sentence.split(' ').map(reverseStr).join(' ');
}
// @param sentence: pass a sentence.
function reverseSentence(s){
return s.split(' ').reverse().join(' ');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment