Code Challenge - 2024-06-18

 avatar
unknown
javascript
a year ago
978 B
9
Indexable
const timeLookup = {
    '[': 30,
    ']': 30,
    '(': 15,
    ')': 15,
    '{': 5,
    '}': 5,
    ' ': 0,
}

function digging(input) {
    if (!Array.isArray(input)) {
        throw "Input must be array"
    }

    return input.reduce((carry,section)=>{
        return carry 
            // Handle Dirt Carry Out
            + (section.includes(' ') ? 10 : 20)
            // Section Cost
            + section.split('').reduce((carry2,material)=>{
                return carry2 + timeLookup[material];
            }
            , 0);
    }
    , 0)

}

const testCases = [{
    test: ['{}', '[]', '()'],
    result: 160
}, {
    test: ['{ ', '[ ', ' )'],
    result: 80
}, {
    test: ['[}', '(]'],
    result: 120
}]

// Run Tests
testCases.forEach(tc => {
    const result = digging(tc.test);
    console.log("Test: ", tc.test, 
                "Expected:", tc.result, 
                "Actual:", result, 
                " - ", result == tc.result ? 'PASS' : 'FAIL');
})
Editor is loading...
Leave a Comment