-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinwords.js
More file actions
35 lines (29 loc) · 968 Bytes
/
spinwords.js
File metadata and controls
35 lines (29 loc) · 968 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
32
33
/*
Question => Write a function that takes in a string of one or more words,
and returns the same string, but with all words that have five or
more letters reversed (Just like the name of this Kata).
Strings passed in will consist of only letters and spaces.
Spaces will be included only when more than one word is present.
*/
function spinWords(str) {
const strArr = str.split(" ");
console.log(strArr);
if (strArr.length <= 1) {
const str = strArr.join("");
if (str.length <= 4) {
return str;
}
return str.split("").reverse().join("");
}
let text = "";
for (let i = 0; i < strArr.length; i++) {
let _str = strArr.slice(i, i + 1);
if (_str.join("").length < 5) {
text += _str.join("") + " ";
}else {
text += _str.join("").split("").reverse().join("");
}
}
return text.trim();
}
const check = spinWords("This is a sentence");