Problem
- Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed.
- Strings passed in will consist of only letters and spaces.
- Spaces will be included only when more than one word is present.
- 공백은 둘 이상의 단어가 있을 경우에만 포함된다.
Solution 01
function spinWords(str) {
let arr = str.split(' ');
for (let i = 0; i < arr.length; i++) {
if (arr[i].length > 4) {
arr[i] = arr[i].split('').reverse().join('');
}
}
return arr.join(' ');
}
spinWords('abcd'); // abcd
spinWords('abcde'); // edcba
spinWords('How are you brother'); // How are you rehtorb
Solution 02
function spinWords(str) {
return str.split(' ').map(i => i.length > 4 ? i.split('').reverse().join('') : i).join(' ');
}
spinWords('abcd'); // abcd
spinWords('abcde'); // edcba
spinWords('How are you brother'); // How are you rehtorb
Solution 03
function spinWords(str) {
return str.replcae(/\w{5,}/g, function(word) {
return word.split('').reverse().join('');
});
}
spinWords('abcd'); // abcd
spinWords('abcde'); // edcba
spinWords('How are you brother'); // How are you rehtorb