Skip to content

Commit e0789bc

Browse files
abhi0977itsvinayak
andauthored
Add reverse words (#162)
* add reverse words * Update ReverseWords.js * Update ReverseWords.js Co-authored-by: vinayak <[email protected]>
1 parent f2ab68e commit e0789bc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

String/ReverseWords.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const reverseWords = (str) => {
2+
// Split string into words
3+
// Ex. "I Love JS" => ["I", "Love", "JS"]
4+
const words = str.split(' ')
5+
// reverse words
6+
// ["I", "Love", "JS"] => ["JS", "Love", "I"]
7+
const reversedWords = words.reverse()
8+
// join reversed words with space and return
9+
// ["JS", "Love", "I"] => "JS Love I"
10+
return reversedWords.join(' ')
11+
}
12+
13+
// testing
14+
console.log(reverseWords('I Love JS'))
15+
console.log(reverseWords('My Name Is JavaScript'))

0 commit comments

Comments
 (0)