-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate Inventory
49 lines (41 loc) · 1.17 KB
/
Update Inventory
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
function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
// check if new item exists
for (let i = 0; i < arr2.length; i++) {
let newitemname = arr2[i][1];
let itemexists = false;
for (let j = 0; j < arr1.length; j++) {
if (newitemname == arr1[j][1]) {
//exists!
arr1[j][0] += arr2[i][0];
itemexists = true;
}
}
if (itemexists == false) {
//doesn't exist, find the correct alphabetical location and add.
let alphabeticallylast = true;
for (let k = 0; k < arr1.length; k++) {
if (newitemname < arr1[k][1]) {
arr1.splice(k, 0, arr2[i]);
alphabeticallylast = false;
}
}
}
}
console.log(arr1);
return arr1;
}
// 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);