Skip to content
Browse files

Update Algorithm-Inventory-Update.md

change cur to item

Sorry about this, didn't see the note below
  • Loading branch information...
1 parent da84035 commit 2a4f0e6f576403039aef451d347a17815348c06e @imranismail imranismail committed with Rafase282
Showing with 28 additions and 0 deletions.
  1. +28 −0 Algorithm-Inventory-Update.md
View
28 Algorithm-Inventory-Update.md
@@ -133,6 +133,26 @@ function inventory(arr1, arr2) {
});
}
```
+
+#### Fourth Solution
+```js
+//jshint esversion: 6
+function inventory(curInv, newInv) {
+ var inv = new Map();
+
+ [...curInv, ...newInv].forEach(item => {
+ if(inv.has(item[1]))
+ inv.set(item[1], inv.get(item[1]) + item[0]);
+ else
+ inv.set(item[1], item[0]);
+ });
+
+ return [...inv]
+ .map(item => [item[1], item[0]])
+ .sort((a, b) => a[1] > b[1] ? 1 : -1);
+}
+```
+
# Code Explanation:
#### First solution
- Start by creating a variable to store the index in. Define variables outside of loops
@@ -148,6 +168,14 @@ function inventory(arr1, arr2) {
#### Second and Third solutions
- Read comments in code.
+#### Fourth solution
+- Start by creating the `inv` [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) to store the updated inventory
+- Combine both `curInv` and `newInv` and iterate through
+- Check if `inv` has the key, update it's value if `inv` has them. Otherwise store the new value
+- Use ES6 spread operator to convert `inv` map to a 2d array
+- Map through the array to change it's location to be [value, key] as needed by the challenge
+- Sort the array alphabetically
+
## Related links
- [Function.prototype.call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call)
- [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)

0 comments on commit 2a4f0e6

Please sign in to comment.
Something went wrong with that request. Please try again.