시저 암호

김현민·2021년 9월 3일
0

Algorithm

목록 보기
62/126
post-thumbnail

내 코드

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('');
}

풀이 과정

  1. string을 split.

  2. toAscii 배열에 ascii코드로 변환(charCodeAt)된 코드를 push

  3. 문제 조건에 따라 ascii --> String으로 변환(fromCharCode) 후 result 배열에 push

  4. join

profile
Jr. FE Dev

0개의 댓글