function solution(s, n) {
var splitted = s.split('');
var toAscii=[];
var result = [];
for(let i = 0 ; i<splitted.length ; i++){
toAscii.push(splitted[i].charCodeAt(0))
}
for(let i = 0; i<toAscii.length ; i++){
if(toAscii[i] <= 90 &&toAscii[i] >= 65 && toAscii[i] + n > 90){
result.push(String.fromCharCode(toAscii[i] + n - 26))
}else if(toAscii[i] >= 97 && toAscii[i] <= 122 && toAscii[i] + n > 122){
result.push(String.fromCharCode(toAscii[i] + n - 26))
}else{
//공백 처리
if(toAscii[i]===32) result.push(String.fromCharCode(toAscii[i]));
else result.push(String.fromCharCode(toAscii[i] + n))
}
}
return result.join('');
}
string을 split
.
toAscii 배열에 ascii코드로 변환(charCodeAt
)된 코드를 push
문제 조건에 따라 ascii --> String으로 변환(fromCharCode
) 후 result 배열에 push
join