Jun 개발노트

프로그래머스 - 시저암호

2020-01-26

시저암호

const changeCode = (s, n) => {
    const codeArr = [];
    let answer = "";
    let result;
    let changeString = String.fromCodePoint;
    for (let i in s) {
        codeArr.push(s.codePointAt(i));
    }
    for (let value of codeArr) {
        let count = value === 32 ? 0 : n;
        result = value + count;
        answer +=
            value <= 90
                ? changeString(result > 90 ? result - 26 : result)
                : changeString(result > 122 ? result - 26 : result);
    }
    return answer;
};

test("getString", () => {
    expect(changeCode("a B z y", 4)).toEqual("e F d");
});