πŸ’Ž

ARR PS - seven dwarfs

notion image
Β 
notion image
Β 

풀어보기

μΌκ³±λ‚œμž₯이인데 9λͺ…이 μ™”λ‹€. 2λͺ…이 μ˜€λ²„λ˜λ‹ˆκΉŒ 2λͺ…μ”© κΊΌλ‚΄κ³  λ‚œ λ‹€μŒ μ‘°μ‚¬ν–ˆλ‹€.
7λͺ…을 κ³¨λΌμ„œ ν•  ν•„μš” 없이 2λͺ…μ”© μ œμ™Έμ‹œν‚€κ³  7λͺ…μ˜ 합을 reduce λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄ 계산해 ν’€μ—ˆλ‹€.
function answer(dwarf) {
    let result = [];
    for (let i = 0; i < dwarf.length; i++) {
        for (let j = 0; j < dwarf.length; j++) {
						let tmp = dwarf
                    .filter((v, idx) => i !== idx && j !== idx);
            if (tmp.reduce((cur, acc) => cur + acc) === 100) {
                result = tmp;
            }
        }
    }
    return result;
}