๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์•”ํ˜ธ ํ•ด๋… ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์•”ํ˜ธ ํ•ด๋…

Eun_zii 2024. 3. 12. 18:07

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ์•”ํ˜ธ ํ•ด๋…

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ๊ตฐ ์ „๋žต๊ฐ€ ๋จธ์“ฑ์ด๋Š” ์ „์Ÿ ์ค‘ ์ ๊ตฐ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์•”ํ˜ธ ์ฒด๊ณ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค.
  • ์•”ํ˜ธํ™”๋œ ๋ฌธ์ž์—ด cipher๋ฅผ ์ฃผ๊ณ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ๊ทธ ๋ฌธ์ž์—ด์—์„œ code์˜ ๋ฐฐ์ˆ˜ ๋ฒˆ์งธ ๊ธ€์ž๋งŒ ์ง„์งœ ์•”ํ˜ธ์ž…๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด cipher์™€ ์ •์ˆ˜ code๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ํ•ด๋…๋œ ์•”ํ˜ธ ๋ฌธ์ž์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • 1 ≤ cipher์˜ ๊ธธ์ด ≤ 1,000
  • 1 ≤ code  cipher์˜ ๊ธธ์ด
  • cipher๋Š” ์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ณต๋ฐฑ๋„ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’š ํ’€์ด

function solution(cipher, code) {
    const answer = [];
    for ( let i = code - 1; i < cipher.length; i += code) {
        answer.push(cipher[i])
    }
    return answer.join("")
}

 

โœ… 

์šฐ์„  answer์„ ๋นˆ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€ ํ›„, i์— code-1์„ ํ•ด์ฃผ์–ด์•ผ index3 ์ฆ‰ ๋ฌธ์ž์—ด ๋„ค ๋ฒˆ์งธ๋ถ€ํ„ฐ ์ ์šฉ์ด ๋˜๊ธฐ์—

code - 1์„ ํ•ด์ค€ํ›„ code๊ฐ„๊ฒฉ๋งŒํผ answer์— ์ถ”๊ฐ€ํ•ด์ค€ํ›„ join(" ") ๋ฉ”์„œ๋“œ๋กœ ๋ฐฐ์—ด๋กœ ๋˜์–ด์žˆ๋˜

cipher[i]๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด return ํ•ด์ค€๋‹ค. 

728x90