프로그래머스 이상한 문자 만들기 자바스크립트

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"))