[Lv.0] 암호 해독 *

01수정·2022년 11월 18일
0
post-thumbnail

<입문 100문제> Day 14 - 조건문, 반복문, 시뮬레이션, 문자열

문제


풀이

(1) reduce 사용

function solution(cipher, code) {
    return [...cipher.split('')].reduce((result, char, idx) => { return result = (idx+1) % code === 0 ? result += char : result }, '')
}

(2) filter 사용

function solution(cipher, code) {
    return [...cipher.split('')].filter((char, idx) => (idx+1) % code === 0 ).join('')
}

해답

const solution = (cipher, code) => [...cipher].filter((a,i) => (i+1)%code === 0).join("")

참고자료

profile
새싹 FE 개발자

0개의 댓글