👩🏻💻 JS coding test | 알고리즘 Level1
문제
📎
[프로그래머스 - 시저암호](https://programmers.co.kr/learn/courses/30/lessons/12926)
💻 작성코드 1
function solution(s, n) {
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
console.log(upper.length)
const lower = "abcdefghijklmnopqrstuvwxyz"
const length = upper.length
let answer = ''
for (let i = 0; i < s.length; i++) {
let position = upper.includes(s[i]) ? upper : lower.includes(s[i]) ? lower : null
if (position) {
console.log(position)
const idx = position.indexOf(s[i]) + n
console.log(idx)
answer += idx >= length ? position[idx - length] : position[idx]
} else {
answer += " "
}
}
return answer;
}
💡풀이
**indexOf, includes**
알파벳의 소문자 대문자 각각 length와 index를 구한 후 for문으로 값을 구한다.
**charCodeAt, fromCharCode**