๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‘˜๋งŒ์˜ ์•”ํ˜ธ

728x90

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค lev.1 ๋‘˜๋งŒ์˜ ์•”ํ˜ธ

function solution(s, skip, index) {
  const origin = [];
  for (let i = 97; i < 123; i++) {
    origin.push(String.fromCharCode(i));
  }
  const table = origin.filter((v) => ![...skip].includes(v));
  return [...s].map((char) => {
      return table[(table.findIndex((v) => v == char) + index) % table.length];
    }).join('');
}

 

 

 

function solution(s, skip, index) {
  //a-> z ๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
  //a=> 97, z=> 122
  const origin = [];
  for (let i = 97; i < 123; i++) {
    origin.push(String.fromCharCode(i));
  }
  //๋ฐฐ์—ด์—์„œ skip ์— ํฌํ•จ๋œ ๋‹จ์–ด๋Š” ์ง€์šด table ์„ ๋งŒ๋“ ๋‹ค.
  const table = origin.filter((v) => ![...skip].includes(v));
  console.log(table);
  //   [
  //   'a', 'c', 'e', 'f', 'g',
  //   'h', 'i', 'j', 'k', 'l',
  //   'm', 'n', 'o', 'p', 'r',
  //   's', 't', 'u', 'v', 'x',
  //   'y', 'z'
  // ]
  //s ์—ด์— ์žˆ๋Š” ๋ฌธ์ž๋“ค์— index ๋ฅผ ๋”ํ•ด orgin length ๋งŒํผ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€์˜ index ๋ฅผ ์ฐพ๋Š”๋‹ค.
  return [...s]
    .map((char) => {
      return table[(table.findIndex((v) => v == char) + index) % table.length];
    })
    .join('');
}
console.log(solution('aukks', 'wbqd', 5));

ํ’€์ด ๋ฐฉ๋ฒ•. 

 

๐Ÿฅ• findIndex ์—๋Š” ์ธ์ž๋กœ ์ฐธ์ด๋˜๋Š” ์กฐ๊ฑด์ด ๋“ค์–ด์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.

728x90
๋ฐ˜์‘ํ˜•