Inventory Update problem - all tests failing, but should pass based on VS code console

My function below is not passing any test in the Inventory Update problem, despite returning the correct output (array of length 6) and correct values according to VS code console.

My browser is Firefox 109.0. I’ve also tried disabling adblockers

Here’s my code:

function updateInventory(arr1, arr2) {
    let myInv = {};
    
    // create myInv object from arr1 array
    for (idx = 0; idx < arr1.length; idx++) {
        myInv[arr1[idx][1]] = arr1[idx][0];
    }

    // update myInv object with arr2
    for (idx = 0; idx < arr2.length; idx++) {
        if (myInv[arr2[idx][1]] == undefined){
            myInv[arr2[idx][1]] = arr2[idx][0];
        }
        else {
            myInv[arr2[idx][1]] = myInv[arr2[idx][1]] + arr2[idx][0];
        }
    }

    arr1 = Object.entries(myInv); // convert the myInv object to array and assign to arr1
    arr1.sort(); // sort the arr1 array alphabetically

    // invert each item in arr1 array, from [name, qty] to [qty, name] format
    for (idx = 0; idx < arr1.length; idx++) {
        arr1[idx] = [arr1[idx][1], arr1[idx][0]];
    }

    return arr1;
}

VS code output for test#3

I would appreciate if someone took a look what’s wrong

I found what’s wrong. All tests are passing now. I did not write let, as in

for (let idx = 0;

So the engine throws an error (which I didn’t notice at first) saying I have to declare idx first before assigning to it. Not sure why VS code doesn’t complain about this.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.