mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
✨ Add "inventory-update" challenge
This commit is contained in:
parent
565d95079f
commit
8be530c08d
11
challenges/inventory-update/README.md
Normal file
11
challenges/inventory-update/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# inventory-update
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) at 8 July 2020.
|
||||||
|
|
||||||
|
## Instructions :
|
||||||
|
|
||||||
|
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 `array1`). 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.
|
||||||
|
|
||||||
|
## Examples :
|
||||||
|
|
||||||
|
See the `input-output.json` file for examples of input/output.
|
50
challenges/inventory-update/input-output.json
Normal file
50
challenges/inventory-update/input-output.json
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"input": [
|
||||||
|
[
|
||||||
|
{ "quantity": 21, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 2, "item": "Dirty Sock" },
|
||||||
|
{ "quantity": 1, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 5, "item": "Microphone" }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "quantity": 2, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 3, "item": "Half-Eaten Apple" },
|
||||||
|
{ "quantity": 67, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 7, "item": "Toothpaste" }
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"output": [
|
||||||
|
{ "quantity": 88, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 2, "item": "Dirty Sock" },
|
||||||
|
{ "quantity": 3, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 3, "item": "Half-Eaten Apple" },
|
||||||
|
{ "quantity": 5, "item": "Microphone" },
|
||||||
|
{ "quantity": 7, "item": "Toothpaste" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"input": [
|
||||||
|
[
|
||||||
|
{ "quantity": 0, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 0, "item": "Dirty Sock" },
|
||||||
|
{ "quantity": 0, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 0, "item": "Microphone" }
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{ "quantity": 1, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 1, "item": "Half-Eaten Apple" },
|
||||||
|
{ "quantity": 1, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 1, "item": "Toothpaste" }
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"output": [
|
||||||
|
{ "quantity": 1, "item": "Bowling Ball" },
|
||||||
|
{ "quantity": 0, "item": "Dirty Sock" },
|
||||||
|
{ "quantity": 1, "item": "Hair Pin" },
|
||||||
|
{ "quantity": 1, "item": "Half-Eaten Apple" },
|
||||||
|
{ "quantity": 0, "item": "Microphone" },
|
||||||
|
{ "quantity": 1, "item": "Toothpaste" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
0
challenges/inventory-update/solutions/.gitkeep
Normal file
0
challenges/inventory-update/solutions/.gitkeep
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# javascript-inventory - inventory-update
|
||||||
|
|
||||||
|
Programming language : JavaScript
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) at 8 July 2020.
|
@ -0,0 +1,16 @@
|
|||||||
|
function solution (array1, array2) {
|
||||||
|
const result = [...array1]
|
||||||
|
|
||||||
|
array2.forEach(delivery2 => {
|
||||||
|
const foundResultIndex = result.findIndex(
|
||||||
|
deliveryResult => deliveryResult.item === delivery2.item
|
||||||
|
)
|
||||||
|
if (foundResultIndex === -1) return result.push(delivery2)
|
||||||
|
return (result[foundResultIndex].quantity =
|
||||||
|
result[foundResultIndex].quantity + delivery2.quantity)
|
||||||
|
})
|
||||||
|
|
||||||
|
return result.sort((a, b) => a.item.localeCompare(b.item))
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = solution
|
Loading…
Reference in New Issue
Block a user