-
[Codewars] Stop gninnipS My sdroW!Algorithm 2021. 4. 1. 20:03
www.codewars.com/kata/5264d2b162488dc400000001/train/javascript
Codewars: Achieve mastery through challenge
Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.
www.codewars.com
더보기입력 받은 문자열 중에 5개 이상의 단어만 뒤집어서 리턴하는 문제
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 (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") => "Hey wollef sroirraw"
spinWords("This is a test") => "This is a test"
spinWords("This is another test") => "This is rehtona test"입력 받은 문자열 중에 5개 이상의 단어만 뒤집어서 리턴하는 문제
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 (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") => "Hey wollef sroirraw"
spinWords("This is a test") => "This is a test"
spinWords("This is another test") => "This is rehtona test"풀이
- 입력 받은 문자열을 띄어쓰기 기준으로 나누어 배열로 만든다
- 배열의 길이만큼 반복문 돌면서 길이가 5개인 인덱스의 문자만 뒤집는다(배열로 만들고 뒤집고 다시 문자로 바꿈)
- 변경된 배열을 문자열로 다시 만들고 리턴
function spinWords(a){ let arr = a.split(' '); for(let i = 0; i < arr.length; i++) { if(arr[i].length >= 5) { arr[i] = arr[i].split('').reverse().join(''); } } return arr.join(' '); }
'Algorithm' 카테고리의 다른 글
[프로그래머스] 다음 큰 숫자 (0) 2021.04.03 [프로그래머스] 올바른 괄호 (0) 2021.04.02 [프로그래머스] 기능개발 (0) 2021.03.30 [Codewars] Bit Counting (0) 2021.03.29 [Codewars] RGB To Hex Conversion (0) 2021.03.23