mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
✨ Add "inventory-update" challenge
This commit is contained in:
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
|
Reference in New Issue
Block a user