728x90
반응형
charCodeAt 과 fromCharCode + helper 함수를 사용한 코드
접근 방식
[!] : 대문자는 대문자끼리 소문자는 소문자끼리 변환이 되어야함
function solution(s, n) {
let asciiCode=[]
let res;
for(let i=0;i<s.length;i++) {
asciiCode.push(s.charCodeAt(i))
}
let chaged=checkAsciiCode(asciiCode,n)
res=chaged.map((i)=>String.fromCharCode(i)).join('')
return res
}
const checkAsciiCode=(asciiCode,n)=>{
let chaged=asciiCode.map((i)=>{
let plus=i+n
if(i<91 && i+n>90){
i=64
i+=plus-90
}
else if(i>96 && i<123 && i+n > 122){
i=96
i+=plus-122
}
else if(i===32){
i=32
}
else{
i+=n
}
return i
})
return chaged
}
728x90
반응형
'알고리즘(Algorithm)' 카테고리의 다른 글
프로그래머스 - 문자열 나누기[JS] (0) | 2022.12.19 |
---|---|
프로그래머스 - 가장 가까운 글자 [JS] (0) | 2022.12.18 |
unsigned int (0) | 2022.10.04 |
그래프 순회 (0) | 2022.09.10 |
그래프 (0) | 2022.08.31 |