반응형
문제 요약
- 인덱스에 따라서 대소문자 변경
- 인덱스는 공백에서 초기화
문제 풀이
1차 풀이
function solution(s) {
s.split(" ")
.map((i) =>
i
.split("")
.map((c, i) => (i % 2 ? c.toLowerCase() : c.toUpperCase()))
.join("")
)
.join(" ");
}
2차 풀이
function solution(s) {
let index = 0
let result = ""
for(let i = 0; i < s.length; i++){
if(s[i] === " ") {
index = 0
result += " "
}
else {
result += index % 2 ? s[i].toLowerCase() : s[i].toUpperCase()
index++
}
}
return result
}
결론
1차와 2차의 실행속도가 2배이상 차이가 난다.
2중 루프는 사용하지 않는 습관을 들여야 겠다.
반응형
'개발관련 > 매일 코딩 테스트 챌린지' 카테고리의 다른 글
[프로그래머스] 자연수 뒤집어 배열로 만들기 (JavaScript) (0) | 2020.09.08 |
---|---|
[코딜리티] NumberOfDiscIntersections (JavaScript) (0) | 2020.09.07 |
[코딜리티] MaxProductOfThree (JavaScript) (0) | 2020.08.28 |
[프로그래머스] 시저 암호 (JavaScript) (0) | 2020.08.26 |
[코딜리티] MinAvgTwoSlice (javascript) (0) | 2020.08.25 |