シーザーのパスワード



マイコード

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

解法


  • 弦楽split.

  • toAscii配列のASciiコードをcharCodeAt(push)に変換

  • 問題条件に従ってASCII->String(fromCharCode)に変換し、result配列にpushを表示します.
  • join