-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathstop-gninnips-my-sdrow.js
69 lines (59 loc) · 1.32 KB
/
stop-gninnips-my-sdrow.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function reverseString(input) {
return input.split('').reverse().join('');
}
function spinWords(sentence){
let result = '';
const words = sentence.split(' ');
for (let i = 0; i < words.length; i++) {
let word = words[i];
if (word.length >= 5) {
word = reverseString(word);
}
result += ' ' + word;
}
return result.trim();
}
function spinWords(sentence){
let result = '';
const words = sentence.split(' ');
for (let word of words) {
if (word.length >= 5) {
word = reverseString(word);
}
result += ' ' + word;
}
return result.trim();
}
function spinWords(sentence){
return sentence.split(' ').map(word => {
if (word.length >= 5) {
return reverseString(word);
}
return word;
}).join(' ');
}
function spinWords(sentence){
return sentence.split(' ').reduce((result, word) => {
if (word.length >= 5) {
word = reverseString(word);
}
result += ' ' + word;
return result;
}, '').trim();
}
function spinWords(sentence){
return sentence.split(' ').reduce((result, word, i) => {
if (word.length >= 5) {
word = reverseString(word);
}
if (i == 0) {
result += word;
} else {
result += ' ' + word;
}
return result;
}, '');
}
console.log(
spinWords("Hey fellow warriors"),
"Hey wollef sroirraw");