Algorithms - Inventory Update - Code works but fails tests

Tell us what’s happening:
My code returns the correct responses, but it fails a few of the tests for some reason, and I can’t figure out why.

I looked through a few posts from people who had similar issues, but their solutions didn’t work or did not apply.

Can you help figure out why this is not passing?

Thank you!

Your code so far

// Example inventory lists
const currInv = [
    [2, "Hair Pin"], 
    [3, "Half-Eaten Apple"], 
    [67, "Bowling Ball"], 
    [7, "Toothpaste"]
]

const newInv = [
    [21, "Bowling Ball"], 
    [2, "Dirty Sock"], 
    [1, "Hair Pin"], 
    [5, "Microphone"]
] 

function updateInventory(arr1, arr2) {

    // loop through each new item and each current item
     arr2.forEach(newItem => {
        arr1.forEach(curItem => {

            // if new item exists in curr inv, 
            if (curItem[1] === newItem[1]) {

                // update curr item price
                const newPrice = curItem[0] + newItem[0];
                curItem[0] = newPrice;

                // remove new item from new inv list
                const newItemIndex = arr2.indexOf(newItem);
                arr2.splice(newItemIndex, 1);
            };
        });
    });

    // add remaining items from arr2 list to current inventory
    arr1.push(...arr2);

    return arr1;
};

updateInventory(currInv, newInv);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

Challenge: Algorithms - Inventory Update

Link to the challenge:

one issue I immediately noticed is that your output does not satisfy this requirement

The returned inventory array should be in alphabetical order by item.

For eg: I logged the call to the function and this is what it gave:
[ [ 3, ‘Hair Pin’ ],
[ 3, ‘Half-Eaten Apple’ ],
[ 88, ‘Bowling Ball’ ],
[ 7, ‘Toothpaste’ ],
[ 2, ‘Dirty Sock’ ],
[ 5, ‘Microphone’ ] ]

So obviously that is not ordered alphabetically.

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