Code Challenge - April 1, 2024

mail@pastecode.io avatar
unknown
javascript
22 days ago
948 B
8
Indexable
Never
function isBadArray(arr) {
    return arr.some(
        (item)=> /[-.]|\d{2,}/.test(item);
    )
}

function sumArray(arr1, arr2) {
    if (isBadArray(arr1) || isBadArray(arr2)) {
        return null;
    }

    return (parseInt(arr1.join('')) + parseInt(arr2.join(''))).toString().split('');
}

// Test Cases
const tests = [{
    ar1: [1, 2, 3, 9],
    ar2: [1],
    result: [1, 2, 4, 0]
}, {
    ar1: [9, 9, 9, 9],
    ar2: [1],
    result: [1, 0, 0, 0, 0]
}, {
    ar1: [-9, 9, 9, 9],
    ar2: [1],
    result: null
}, {
    ar1: [9, 9.9, 9, 9],
    ar2: [1],
    result: null
}, {
    ar1: [99],
    ar2: [1],
    result: null
}, {
    ar1: [2, 3, 3],
    ar2: [5, 4, 4],
    result: [7, 7, 7]
}]

// Test Harness
for (let test of tests) {
    const result = sumArray(test.ar1, test.ar2);
    console.assert((result === null && test.result === null) || result.join('') == test.result.join(''), 'Failed', test);
}
console.log('All Test Passed')
Leave a Comment