본문 바로가기

■ 프로그래밍/알고리즘

[JS] Stop gninnipS My sdroW!

 

문제

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 (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.

 

Examples:

spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" 
spinWords( "This is a test") => returns "This is a test" 
spinWords( "This is another test" ) => returns "This is rehtona test"

내 풀이

2020.11.13 (20m)

function spinWords(str) { 
  let result = '' 
  const arr = str.split(' ') 
    for (let i = 0; i < arr.length; i++) { 
      if (arr[i].length <= 4) { 
      	result += arr[i] + ' ' 
      } else { 
      	result += arr[i] .split('') .reverse() .join('') + ' ' 
      } 
  	} 
  return result.substring(0, result.length - 1) 
}

 

출처: CodeWars

 

'■ 프로그래밍 > 알고리즘' 카테고리의 다른 글

[JS] Human Readable Time  (0) 2020.11.15
[JS] Vowel Count  (0) 2020.11.14
[JS] Regex validate PIN code  (0) 2020.11.12
[JS] Who likes it?  (0) 2020.11.11
[JS] Mumbling  (0) 2020.11.10