forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordRev3.js
49 lines (42 loc) · 1.15 KB
/
wordRev3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @author MadhavBahlMD
* @date 27/12/2018
*/
// Without inbuilt reverse or split, a straightforward method
function wordRev (line) {
// iterate through the string and add each word in a new array (splitting it on white space)
let words = [], count = 0, word = '';
for (let i=0; i<line.length; i++) {
if (line[i] !== ' ') {
word += line[i];
} else {
words[count] = word;
word = '';
count++;
}
}
// Add the last word as well to the words array as well
words[count] = word;
count++;
// Reverse the words
let temp;
for (let i=0; i<count; i++) {
temp = '';
for (let j=words[i].length-1; j>=0; j--) {
temp += words[i][j];
}
words[i] = temp;
}
// join the elements (Ok, let's not use the traditional join() method -_-)
let reversed = '';
for (let i=0; i<count; i++) {
if (i!=count-1) {
reversed += words[i] + ' ';
} else {
reversed += words[i];
}
}
// print the result
return reversed;
}
console.log(wordRev ('hello world greetings'));