-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseEveryOtherWord.js
More file actions
31 lines (27 loc) · 1014 Bytes
/
reverseEveryOtherWord.js
File metadata and controls
31 lines (27 loc) · 1014 Bytes
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
function reverse(str) {
// result = [];
// let splitString = str.split(' ');
// splitString.forEach((word) => {
// if (splitString.indexOf(word) % 2 === 1) {
// result.push(word.split('').reverse().join(''));
// } else {
// result.push(word);
// }
// });
// if (result.length) {
// return result.join(' ').trim();
// }
// return '';
return str
.trim()
.split(' ')
.map((word) => (str.split(' ').indexOf(word) % 2 === 1 ? word.split('').reverse().join('') : word))
.join(' ');
}
//I guess this could maybe be better done with a Regex
// if you see a string, can you use a regex,
// in general this seems to compensate for the fact that spaces are ignored with the whole split method etc?
console.log(reverse('Reverse this string, please!'));
// console.log(2 % 2);
//The solution I liked had basically a one liner with a map etc and an inline ternary expression. That shit was cool for sure.
//the other one had a for loop starting at one and incrementing by 2 which was also cool