This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 2022.11.08.화요일 | |
const solution = (str) => { | |
const regex = /(\s+)/g; | |
const splitByBlankSpace = str.split(regex); | |
const convertedJadenCase = splitByBlankSpace.map((word) => { | |
const wordLength = word.length; | |
if (wordLength > 0) { | |
return ( | |
word[0].toUpperCase() + word.substring(1, wordLength).toLowerCase() | |
); | |
} | |
return word; | |
}); | |
return convertedJadenCase.join(""); | |
}; |
8번 테케 에러 해결: 마지막 문자에 공백이 있는 경우 오류 떠서 if문으로 처리해줌
split 연산자 String.prototype.split() - JavaScript | MDN (mozilla.org) 메서드 특징 이용해서 풀이
Splitting with a RegExp to include parts of the separator in the result
If separator is a regular expression that contains capturing parentheses ( ), matched results are included in the array.
const myString = "Hello 1 word. Sentence number 2.";
const splits = myString.split(/(\d)/);
console.log(splits);
// [ "Hello ", "1", " word. Sentence number ", "2", "." ]
원래는 split 메서드는.. 지정한 seperator를 칸막이로 사용하여 배열로 바꾼다고 생각하면 됨
\d → 숫자로 쪼갬, 원래는 칸막이는 포함 안됨
근데 regex를 괄호로 감싸주면 해당 칸막이 (숫자)까지 배열에 포함됨
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 Lv.2] 영어 끝말잇기 JavaScript 풀이 (0) | 2022.12.07 |
---|---|
[프로그래머스 Lv 2] 행렬의 곱셈 JavaScript 풀이 (0) | 2022.12.05 |
[프로그래머스 Lv 2] 카펫 JavaScript 풀이 (0) | 2022.12.03 |
[프로그래머스 Lv 2] 기능개발 JavaScript 풀이 + 리팩토링 시도.. (0) | 2022.12.01 |
[프로그래머스 Lv2] 롤케이크 자르기 JavaScript 풀이 (0) | 2022.11.28 |