Skip to content

Commit 1a6949c

Browse files
authored
Create reverse_words.js
1 parent 57d30d4 commit 1a6949c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

algorithms/reverse_words.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
Reverse each word of string and return new string
4+
Don't reverse the whole string just each word separately.
5+
Don't use array reverse() function
6+
7+
*/
8+
9+
function reverseWords(str){
10+
11+
let strArr = str.split(" ");
12+
13+
for (let i=0; i < strArr.length; i++) {
14+
15+
const word = strArr[i];
16+
let reverseWord = "";
17+
18+
for ( let j=word.length - 1; j >= 0; j--) {
19+
reverseWord += word[j];
20+
}
21+
22+
/*
23+
OR
24+
25+
for ( let j=0; j < word.length; j++) {
26+
reverseWord += word[word.length - (j+1)];
27+
}
28+
*/
29+
30+
31+
strArr[i] = reverseWord;
32+
33+
};
34+
35+
return strArr.join(" ");
36+
}
37+
38+
39+
reverseWords('i am here');

0 commit comments

Comments
 (0)