2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: a56138aff60341a09ed6c480
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 库存更新
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: inventory-update
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
比较并更新存储在2D阵列中的库存与新交付的第二个2D阵列。更新当前现有库存物料数量(在`arr1` )。如果找不到商品,请将新商品和数量添加到库存数组中。返回的库存数组应按项目的字母顺序排列。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
函数`updateInventory`应该返回一个数组。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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']
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`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的数组。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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"]]` 。
|
|
|
|
|
|
|
|
|
|
```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']
|
|
|
|
|
]
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])`应该返回`[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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']
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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"]]` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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']
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00: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"]]` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
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']
|
|
|
|
|
]
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --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);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```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);
|
|
|
|
|
```
|