[프로그래머스]Algorithm_23.04.17_시저암호

윤성해·2023년 4월 17일
0

알고리즘

목록 보기
19/22
post-thumbnail

시저 암호

문제

레퍼런스

  1. 알파벳이 섞여있을 때

  2. 나누어져있을 때

  1. reduce 메서드 -> 초기값으로 어떤 타입이든 사용할 수 있다.
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function solution(s, n) {
    return s.split("")
            .reduce((acc, cur) => {
                const words = lower.includes(cur) ? lower : upper;
                let idx = words.indexOf(cur) + n;
                        // console.log(acc,cur,words,idx,words[idx])
        
                if (!words[idx]){
                    idx -= 26;
                }
                return acc + (
                    cur === " " ? " " : words[idx]
                );
    },"")
}
  1. charCodeAt() - 아스키 코드 이용

profile
Slow and steady wins the race.

0개의 댓글