Jieunny์˜ ๋ธ”๋กœ๊ทธ

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์‹œ์ € ์•”ํ˜ธ ๋ณธ๋ฌธ

Study/Coding Test

[JS] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์‹œ์ € ์•”ํ˜ธ

Jieunny 2023. 1. 16. 18:51

๐Ÿ“Œ ๋ฌธ์ œ

์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "AB"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "BC"๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด "DE"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. "z"๋Š” 1๋งŒํผ ๋ฐ€๋ฉด "a"๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.

 

โœ๏ธ ํ’€์ด

function solution(s, n) {
  var answer = '';
  let alphabetS = [];
  let alphabetL = [];

  for(let i=65; i<=90; i++){
    alphabetL.push(String.fromCharCode(i));
  }

  for(let i=97; i<=122; i++){
    alphabetS.push(String.fromCharCode(i));
  }

  for(let i=0; i<s.length; i++){
    if(s[i] === ' '){
      answer += ' ';
    }
    if(alphabetL.includes(s[i])){
      if(alphabetL.indexOf(s[i]) + n < alphabetL.length){
        answer += alphabetL[alphabetL.indexOf(s[i])+n]; 
      }
      else{
        answer += alphabetL[n - (alphabetL.length - alphabetL.indexOf(s[i]))];
      }
    }
    else if(alphabetS.includes(s[i])){
      if(alphabetS.indexOf(s[i]) + n < alphabetS.length){
        answer += alphabetS[alphabetS.indexOf(s[i])+n];
      }
        answer += alphabetS[n - (alphabetS.length - alphabetS.indexOf(s[i]))];
      }
    }
  }

  return answer;
}

โžฐ ํ•œ๋ฐ”ํ€ด ๋Œ์•„์™€์•ผ ํ•˜๋Š” ๊ฑฐ ํ•ญ์ƒ ์ธ์‹ํ•˜๊ธฐ