Inventory Update - how to create a 2D array

I am getting the correct values returned but they are formatted as string instead of an array of arrays. How do I use arr.push() to get a 2D array?

My code so far:

function updateInventory(arr1, arr2) {
    //pseudocode:
    //Get all 2nd element values of first array
    //Check each of those against each of the 2nd element values of 2nd array
      //If there is NO match, add that element's 1st and 2nd value (from 2nd array) to 1st array
      //If there is a match, add to that element's 1st value (in 1st array)

    let newArr = [];
    let remArr = [];

    //Comparing each name in arr1 against all names in arr2. If match, we want to add to total.
    for(var i = 0; i < arr1.length; i++){
      for(var k = 0; k < arr2.length; k++){
        if(arr2[k].indexOf(arr1[i][1]) > -1){
          let newTotal = arr1[i][0] + arr2[k][0];
          newArr.push(newTotal,arr1[i][1])
        }
      }
    }

    let onlyNamesArr2 = [];
    for(var n = 0; n < arr2.length; n++){
       onlyNamesArr2.push(arr2[n][1]);
     }
    for(var m = 0; m < arr1.length; m++){
      if(onlyNamesArr2.indexOf(arr1[m][1]) === -1){
        remArr.push(arr1[m]);
      }
    }


    let onlyNamesArr1 = [];
    for(var a = 0; a < arr1.length; a++){
      onlyNamesArr1.push(arr1[a][1]);
    }

    for(var j = 0; j < arr2.length; j++){
      if(onlyNamesArr1.indexOf(arr2[j][1]) > -1){
        //do nothing
      }else {
        newArr.push([arr2[j]])
      };
    }

    let finalArr = newArr + remArr;


}

// 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);

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update/

And by the way, you finalArr is wrong answer so it won’t be accepted anyway. Try to come up a new solution, ok :slight_smile:

Thanks @dangtu. Do you mean it’s not in alphabetical order…? Or something else is wrong…?

Everything. Do you know how to debug you program? It’s easy. Wrap any suspicious functions, objects, etc into console.log. The browser will give the value that object/function returns in console.

Here, take a look at this:

  • Red line is a debug line.
  • Pink draw is comparison between correct answer and yours.

I think you might be looking in the wrong spot. See this a little lower down:

Ha ha, indeed, the bug supposes to lie in the Toothpaste (Your result return Toothpaste2)

See console.log of finalArr:

If you read from left to right as [number, item], [number, item] etc you can see that toothpaste is 7.

I think my real problem is what I posted in the first place, i.e. finalArr is not posting as a 2D array.

That is the problem I need to solve.

I think you should stop using this + operator for array. It doesn’t have a function as join in array. If you wanna join 2 arrays, let use newArr.concat(remArr). You could return it immediately return newArr.concat(remArr)

Haha - we’re getting closer! :slight_smile:

I think now I have to go back into the creation of newArr and remArr to see how those are being created.

And getting a little closer still…

Next step: must alphabetize then split up the final array into a 2D array of pairs.

You have to create 2Darr somewhere in here before thinking about sort it out.
image