-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory-update.js
48 lines (41 loc) · 1.27 KB
/
inventory-update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Algorithms: Inventory Update
*
* Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
*/
/**
* Inventory Update
*
* @author Mike Mai
* @param {Array[][]} arr1 current inventory
* @param {Array[][]} arr2 new inventory
*/
function updateInventory(arr1, arr2) {
const cache = {};
for (let item of arr1) {
cache[item[1]] = item;
}
for (let item of arr2) {
if (cache[item[1]] === undefined) {
arr1.push(item);
cache[item[1]] = item;
} else {
cache[item[1]][0] += item[0];
}
}
return arr1.sort((a, b) => a[1].localeCompare(b[1]));
}
// 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); // [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]