fullfish 2022. 9. 7. 03:00

코드

function solution(str, shiftNumber) {
  let shift = shiftNumber % 26;
  let resultArr = [];
  let unicodeArr = [];
  for (let i = 0; i < str.length; i++) {
    const unicode = str[i].charCodeAt(0);
    let a = 0;
    if (/[A-Z]/.test(str[i])) {
      if (unicode + shift > 90) a = 26;
      unicodeArr.push(unicode + shift - a);
    } else if (/[a-z]/.test(str[i])) {
      if (unicode + shift > 122) a = 26;
      unicodeArr.push(unicode + shift - a);
    } else unicodeArr.push(unicode);
  }
  unicodeArr.forEach((ele) => resultArr.push(String.fromCharCode(ele)));
  return resultArr.join("");
}

예전에 시저 암호 만들어 놓은게 있어서 그대로 가져다썼다

그런데 해당 문제에서는 n이 25이하라고 했으므로

알파벳 배열을 만들어 놓고 사용하는편이 더 빠르겠다