Skip to content

Instantly share code, notes, and snippets.

@lostvikx
Created December 26, 2021 16:54
Show Gist options
  • Save lostvikx/8aafb3d88dfc7ba8924d6ecb14fa0cb3 to your computer and use it in GitHub Desktop.
Save lostvikx/8aafb3d88dfc7ba8924d6ecb14fa0cb3 to your computer and use it in GitHub Desktop.
const updateInventory = (arr1, arr2) => {
const upInv = arr1.slice();
for (const newItem of arr2) {
let found = false;
for (const curItem of arr1) {
if (curItem[1] === newItem[1]) {
found = true;
curItem[0] += newItem[0];
break;
}
}
if (!found) {
// console.log("not found", newItem[1]);
upInv.push(newItem);
}
}
upInv.sort((item1, item2) => {
return (item1[1]<item2[1])
? -1
: (item1[1]>item2[1])
? 1
: 0
});
return upInv;
}
// Example inventory lists
let curInv = [
[21, "Bowling Ball"],
[2, "Dirty Sock"],
[1, "Hair Pin"],
[5, "Microphone"]
];
let newInv = [
[2, "Hair Pin"],
[3, "Half-Eaten Apple"],
[67, "Bowling Ball"],
[7, "Toothpaste"]
];
console.log(updateInventory(curInv, newInv));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment