-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-words.js
48 lines (36 loc) · 1.39 KB
/
reverse-words.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
/*
You're working on a secret team solving coded transmissions.
Your team is scrambling to decipher a recent message, worried it's a plot to break into a major European National Cake Vault. The message has been mostly deciphered, but all the words are backwards! Your colleagues have handed off the last step to you.
Write a function reverseWords() that takes a string message and reverses the order of the words in-place.
For example:
var message = 'find you will pain only go you recordings security the into if';
reverseWords(message);
// returns: 'if into the security recordings you go only pain will you find'
When writing your function, assume the message contains only letters and spaces, and all words are separated by one space.
*/
function reverseWordsCheating(message) {
return message.split(' ').reverse().join(' ');
}
function reverseWords(message) {
let arr = reverseChars(message.split(''), 0, message.length -1);
function reverseChars(messageArr, start, end) {
while (start < end) {
let tmp = messageArr[start];
messageArr[start] = messageArr[end];
messageArr[end] = tmp;
start++;
end--;
}
return messageArr;
}
let start = 0;
let end = 0;
for (let i = 0; i <= arr.length; i++) {
if (arr[i] === ' ' || i === arr.length) {
end = i - 1;
reverseChars(arr, start, end);
start = i + 1;
}
}
return arr.join('');
}