Inventory Update - not passing tests

Tell us what’s happening:
I am trying to solve the inventory update challenge, but I am not passing the third, fifth and sixth tests even though I get the correct answers locally. I do not have any global variables I am using so I am probably missing something. What am I missing?

Your code so far


function updateInventory(arr1, arr2) {
    let res = [];
    // Update exising items
    for (let i = 0; i < arr1.length; i ++) {
        let existing = arr1[i];
        let newItem = arr2.find(x => x[1] === existing[1])
        res.push(existing);
        if (newItem) {
            res[i][0] = existing[0] + newItem[0]
        }
    }
    // Appnend new items
    for (let newItem of arr2) {
        if (!res.map(x => x[1]).includes(newItem[1]))
            res.push(newItem)
    }
    // Sort by abc
    res = res.sort((a, b) => a[1] > b[1]);
    return res
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

The problem is the way you are trying to sort res. See the following MDN link for what a valid sort compareFunction should look like. You have to account for 3 conditions by returning a different value for each of the following:

  • a[1] less the b[1]
  • a[1] greater than b[1]
  • a[1] equal to b[1]

You can also look into localeCompare method.

1 Like

Amazing, I knew that but was too lazy so I used my short invalid way. Thanks a lot!