본문 바로가기

Coding/내일배움캠프

[내일배움캠프] Node.js 4기 TIL | Day 18 | 24.01.15.(월)

Algorithm / 프로그래머스 / Lv.1 / 12926. 시저 암호 / 시저 암호.js

 

function solution(s, n) {
    var answer = '';
    
    console.log(typeof(s));
    
    let arr = new Array(s.length);
    
    for (let i = 0 ; i < s.length; i++){
        arr[i] = s[i];
        console.log(i + "번째 : " + arr[i]);
    }
  
    for (let i = 0 ; i < s.length; i++){
        // 대문자인 경우
        if (65 <= arr[i].charCodeAt() && arr[i].charCodeAt() <= 90){
            arr[i] = String.fromCharCode(arr[i].charCodeAt() + n);
            if (arr[i].charCodeAt() > 90)
                arr[i] = String.fromCharCode(arr[i].charCodeAt() - 26);
        }
        // 소문자인 경우
        else if (97 <= arr[i].charCodeAt() && arr[i].charCodeAt() <= 122){
            arr[i] = String.fromCharCode(arr[i].charCodeAt() + n);
            if (arr[i].charCodeAt() > 122)
                arr[i] = String.fromCharCode(arr[i].charCodeAt() - 26);
        }
        
        answer += arr[i];
    }

    return answer;
}