프로그래머스 이상한 문자 만들기 자바스크립트
2022. 1. 20. 13:37ㆍ코딩공부/JavaScript 알고리즘

수도 코딩
문자열을 공백기준으로 자르기
0번 index는 toUpperCase 처리
문자열별로 index % 2 == 1 이면 toLowerCase
index % 2 == 0 이면 toUpperCase
공백두고 문자열 세개 다시 합치기
나의 풀이
function solution(s) {
var answer = "";
let splitResult = s.split(' ');
let temp2 = [];
for(let i=0; i<splitResult.length; i++) {
let temp = [];
for(let j=0; j< splitResult[i].length; j++) {
if(j%2 == 1) {
temp.push(splitResult[i][j].toLowerCase());
}
if(j%2 == 0) {
temp.push(splitResult[i][j].toUpperCase());
}
}
temp2.push(temp);
}
// 배열에서 문자 빼주기
let result = [];
for(let i =0; i < temp2.length; i++) {
for(let j =0; j < temp2[i].length; j++) {
result += temp2[i][j];
}
if(i != temp2.length -1) {
result += " "; // 마지막에 띄어쓰기 없앰
}
}
answer = result;
return answer;
}
// console.log(solution("try hello world"))
'코딩공부 > JavaScript 알고리즘' 카테고리의 다른 글
프로그래머스 정수 내림차순으로 배치하기 자바스크립트 (0) | 2022.01.20 |
---|---|
프로그래머스 자연수 뒤집어 배열로 만들기 자바스크립트 (0) | 2022.01.20 |
프로그래머스 수박수박수박수박수? 자바스크립트 (0) | 2022.01.20 |
프로그래머스 서울에서 김서방 찾기 자바스크립트 (0) | 2022.01.20 |
프로그래머스 문자열 다루기 기본 자바스크립트 (0) | 2022.01.20 |