[Coding Test] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค -์‹œ์ € ์ฝ”๋“œ-

๐Ÿ“ƒ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/12926

๐Ÿคจ์ƒ๊ฐํ•˜๊ธฐ

ascii ์ฝ”๋“œ ๋ณ€ํ™˜ ๋ฉ”์„œ๋“œ๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ํฐ ์–ด๋ ค์›€ ์—†์ด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

๐Ÿ˜Ž๋‚ด ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function solution(s, n) {
return s
.split('')
.map((letter) => {
const ascii = letter.charCodeAt()
let asciiConv
if (65 <= ascii && ascii <= 90) {
if (90 < ascii + n) {
// ์†Œ๋ฌธ์ž์ธ๋ฐ ๋ฐ€์—ˆ์„ ๋•Œ z์ดˆ๊ณผ
let excess = ascii + n - 91
asciiConv = 65 + excess
return String.fromCharCode(asciiConv)
}
} else if (97 <= ascii && ascii <= 122) {
if (122 < ascii + n) {
// ๋Œ€๋ฌธ์ž์ธ๋ฐ ๋ฐ€์—ˆ์„ ๋•Œ Z์ดˆ๊ณผ
let excess = ascii + n - 123
asciiConv = 97 + excess
return String.fromCharCode(asciiConv)
}
} else if (ascii == 32) {
// ๊ณต๋ฐฑ
return letter
}
// ์ดˆ๊ณผ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
asciiConv = ascii + n
return String.fromCharCode(asciiConv)
})
.join('')
}

๐Ÿšจ์ฃผ์˜ํ•  ์ 

์ฃผ์˜ํ•ด์•ผ ํ•  ๋ถ€๋ถ„์€ ๋ฌธ์ž๋ฅผ ๋ฐ€์—ˆ์„ ๋•Œ z,Z๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ์ด ๊ฒฝ์šฐ, ๊ฐ๊ฐ a,A๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•ด ์ดˆ๊ณผํ•œ ๋งŒํผ ์ด๋™ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.