LHJ

I'm a FE developer.

Level 1. 문제 7

25 May 2020 » js_quiz

Level 1. 문제 7

// 입력
const a = [[1,2,10,3,5], [3,4]]
const b = [[5,6,9,1], [3,7,8]]
solution(a, b);

// 출력
[[6, 8, 19, 4, 5], [6, 11, 8]]
// 내가 푼 답
const solution = (a, b) => {
    let arr = [];
    
    for(let i=0; i<a.length; i++) {
        let max = a[i].length - b[i].length >= 0 ? a[i].length : b[i].length;
        let subarr = [];
        for(let j=0; j<max; j++) {
            let arr1 = a[i].slice(j,j+1)[0]? a[i].slice(j,j+1)[0] : 0;
            let arr2 = b[i][j] ? b[i][j] : 0;
            subarr.push(arr1 + arr2);
        }
        arr.push(subarr)
    }
    return arr;
}

Related Posts