Tell us what’s happening:
For some reason the code below doesn’t pass the test in the challenge.
I’ve compared my result with the expected result in the test and it’s identical.
Using result.toString() === expected.toString() also returns true.
So far I don’t have any clue on what’s wrong with my code, probably I missed something.
Your code so far
const updateItem = (item, newItem) => [item[0] + newItem[0], newItem[1]];
const sortInventory = (arr = []) => arr.sort((a, b) => a[1] > b[1]);
const updateInventory = (arr1 = [], arr2 = []) => {
const updatedInv = arr1.map(item => {
const newItem = arr2.filter(newItem => newItem[1] === item[1])[0];
return newItem ? updateItem(item, newItem) : item;
}).concat(arr2.filter(newItem => {
return !arr1.filter(item => newItem[1] === item[1]).length;
}));
return sortInventory(updatedInv);
}
// Example inventory lists
const curInv = [
[21, "Bowling Ball"],
[2, "Dirty Sock"],
[1, "Hair Pin"],
[5, "Microphone"]
];
const 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36
.
Link to the challenge:
//https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update