Inventory Update - Not able to submit

Tell us what’s happening:
Hi guys, pretty sure I got the answer right, but they’re not letting me complete the lesson for some reason?

Your code so far


function updateInventory(curInv, newInv) {
  let entireInv = curInv;
  newInv.map(item => {
    for (let i = 0; i < entireInv.length; i++) {
      if (item[1] === entireInv[i][1]) {
        entireInv[i][0] = entireInv[i][0] + item[0];
        return undefined;
      }
    }
    entireInv.push(item);
  });

  entireInv.sort((a, b) => a[1] > b[1]);

  return entireInv;
}

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 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

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

what are the tests you are not passing?

are you checking what you are returning?

have you tried debugging your code with console.log() statements?

you can also try a tool like JavaScript Tutor, it is wonderful for beginners

These are the tests I’m not passing.
But its weird because I’ve checked all of them in the test itself, as well as vscode.

Edit: Might it be because I’m using the map function?

it’s certainly not the map function
have you tried seeing if there is any difference in the result using an editor in your browser?

also, do you know that this is not a copy?

I just tried it on code pen and it gave me this result.

Should I use the spread operator then?
let entireInv = [...curInv];

you are missing the alphabetical order of the items!

so maybe your issue is in the sort() method

if you want to copy the array, that would be the way, yes, but even then, as this is a multidmensional array you are copying the outer array but not the inner ones
you could do let entireInv = JSON.parse(JSON.stringify(curInv));
it can be considereded “hacky”, but it is the easiest way to copy a multidimensional array or object, otherwise you would need to copy each sub array separately, like for example let entireInv = curInv.map(x => [...x]) (but if you don’t how many levels you have it doesn’t work)

Hmmm, thanks for pointing out the sort method. It’s strange because it does sort on my console in vscode.

What would be the proper way to copy an array?

spread operator copy an array, but it copys only the highest level array, if it is a multidimensional array the way in my previous post is the only way without creating a complicated (and unnecessary, as there is that way already) algorithm