Inventory Update - System Not Accepting Correct Solution?

Tell us what’s happening:

My code is marked correct for the 2nd/4th test cases depending on whether or not I update the variable curInv with the definition specified by the test case itself. However, it will not mark both test cases as correct, even though I am not changing function updateInventory() in any way, only variable curInv.

I also can’t get the 3rd test case to work, even though I’ve done a direct comparison of my output (the JSON.stringify line using the 2nd test cases variable definitions) with the test cases output and they are exactly the same (well, ‘almost exactly’, as there are spaces after the commas I guess…).

I’ve tried changing the return variable in function updateInventory to arr1 (by setting it to finalArray) too with no luck either.

Does anybody know how I can get this code to be recognized as correct for the 2nd/3rd and 4th test cases? I believe my algorithm is correct, the system just isn’t accepting some aspect.

Your code so far

function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!
    var finalObject = {};
    var finalArray = [];
    var keys;

    curInv = curInv.concat(newInv);

    for (var i = 0; i < curInv.length; i++)
        if (!finalObject[curInv[i][1]])
            finalObject[curInv[i][1]] = curInv[i][0];
            finalObject[curInv[i][1]] += curInv[i][0];

    keys = Object.keys(finalObject).sort();

    for (var i = 0; i < keys.length; i++)
        if (keys[i] != "undefined")
            finalArray.push([finalObject[keys[i]], keys[i]]);


    return finalArray;

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"]

var newInv = [
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]

updateInventory(curInv, newInv);

Your browser information:

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

Link to the challenge:

This line is messing your program: curInv = curInv.concat(newInv);

Your function should not modify global variables. That is a principle of functional programming.

Use the provided arguments, arr1 and arr2, instead. :slight_smile:

1 Like

Oh god. Ok, I understand now. Thanks so much!