Add reverse words (#162)

* add reverse words

* Update ReverseWords.js

* Update ReverseWords.js

Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
Abhi Ramani
2020-05-10 00:25:56 +05:30
committed by GitHub
parent f2ab68e6be
commit e0789bccde

15
String/ReverseWords.js Normal file
View File

@ -0,0 +1,15 @@
const reverseWords = (str) => {
// Split string into words
// Ex. "I Love JS" => ["I", "Love", "JS"]
const words = str.split(' ')
// reverse words
// ["I", "Love", "JS"] => ["JS", "Love", "I"]
const reversedWords = words.reverse()
// join reversed words with space and return
// ["JS", "Love", "I"] => "JS Love I"
return reversedWords.join(' ')
}
// testing
console.log(reverseWords('I Love JS'))
console.log(reverseWords('My Name Is JavaScript'))