2021-05-05 10:13:49 -07:00
|
|
|
---
|
|
|
|
id: a56138aff60341a09ed6c480
|
2021-09-18 11:22:53 -07:00
|
|
|
title: 庫存更新
|
2021-05-05 10:13:49 -07:00
|
|
|
challengeType: 5
|
|
|
|
forumTopicId: 16019
|
|
|
|
dashedName: inventory-update
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
將存儲在二維數組中的庫存與新交貨的二維數組進行比較和更新。 更新當前現有庫存物料數量(在 `arr1`)。 如果找不到商品,請將新商品和數量添加到庫存數組中。 返回的庫存數組應按項目的字母順序排列。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
函數 `updateInventory` 應該返回一個數組。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.isArray(
|
|
|
|
updateInventory(
|
|
|
|
[
|
|
|
|
[21, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[5, 'Microphone']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[2, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[67, 'Bowling Ball'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` 應該返回一個長度爲 6 的數組。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(
|
|
|
|
updateInventory(
|
|
|
|
[
|
|
|
|
[21, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[5, 'Microphone']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[2, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[67, 'Bowling Ball'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
).length,
|
|
|
|
6
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` 應返回`[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(
|
|
|
|
updateInventory(
|
|
|
|
[
|
|
|
|
[21, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[5, 'Microphone']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[2, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[67, 'Bowling Ball'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
),
|
|
|
|
[
|
|
|
|
[88, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[3, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[5, 'Microphone'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])`應該返回`[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(
|
|
|
|
updateInventory(
|
|
|
|
[
|
|
|
|
[21, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[5, 'Microphone']
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
[
|
|
|
|
[21, 'Bowling Ball'],
|
|
|
|
[2, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[5, 'Microphone']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
`updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` 應該返回 `[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(
|
|
|
|
updateInventory(
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
[2, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[67, 'Bowling Ball'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
),
|
|
|
|
[
|
|
|
|
[67, 'Bowling Ball'],
|
|
|
|
[2, 'Hair Pin'],
|
|
|
|
[3, 'Half-Eaten Apple'],
|
|
|
|
[7, 'Toothpaste']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
`updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])` 應返回 `[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert.deepEqual(
|
|
|
|
updateInventory(
|
|
|
|
[
|
|
|
|
[0, 'Bowling Ball'],
|
|
|
|
[0, 'Dirty Sock'],
|
|
|
|
[0, 'Hair Pin'],
|
|
|
|
[0, 'Microphone']
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[1, 'Half-Eaten Apple'],
|
|
|
|
[1, 'Bowling Ball'],
|
|
|
|
[1, 'Toothpaste']
|
|
|
|
]
|
|
|
|
),
|
|
|
|
[
|
|
|
|
[1, 'Bowling Ball'],
|
|
|
|
[0, 'Dirty Sock'],
|
|
|
|
[1, 'Hair Pin'],
|
|
|
|
[1, 'Half-Eaten Apple'],
|
|
|
|
[0, 'Microphone'],
|
|
|
|
[1, 'Toothpaste']
|
|
|
|
]
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function updateInventory(arr1, arr2) {
|
|
|
|
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);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function updateInventory(arr1, arr2) {
|
|
|
|
arr2.forEach(function(item) {
|
|
|
|
createOrUpdate(arr1, item);
|
|
|
|
});
|
|
|
|
// All inventory must be accounted for or you're fired!
|
|
|
|
return arr1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createOrUpdate(arr1, item) {
|
|
|
|
var index = -1;
|
|
|
|
while (++index < arr1.length) {
|
|
|
|
if (arr1[index][1] === item[1]) {
|
|
|
|
arr1[index][0] += item[0];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (arr1[index][1] > item[1]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arr1.splice(index, 0, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
```
|