Tell us what’s happening:
several tests are failing when I submit this code, though when I check the output in a repl it seems to be correct. what am I doing wrong here?
Your code so far
function updateInventory(arr1, arr2) {
let stock = [...arr1];
arr2.forEach(i=> {
let t = stock.findIndex(x=> x[1]===i[1]);
if (t >= 0) stock[t][0] += i[0];
else stock.push(i)
})
stock.sort((a,b)=> a[1] > b[1]);
return stock;
}
// 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 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
.
Challenge: Inventory Update
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/inventory-update