View on GitHub

marymafa.github.io


layout: post title: Inventory Update date: 2018-01-08 —

Inventory Update

Algorithem challenge:

I were asked to 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 arr1). 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.

Code explanation:

Create a new array that will will store the results after concating array one and array two. Have areduce methode that will will add array and array two if both array have the same item.

Problem started :

I used for each statement to compare each element of the first array and the the second array and if there are the same element like 2 Hair pin and 3hair then it suppose to add those elements like it should be 5 hair pin instead of 2 hair pin and 3 hair pin in the same array but my code didn’t add those elements.

My Approach :

I created a function title MergingTwoArray that will merge array one and array two into a single unit.I created another fuction titled updateInventory that will compare array one and array two if contain the same elements. I also created two variables the first one called current inventory that stores current elements and the last one called new inventory that stores new elements.

Conclusion:

I managed to merge two array and it was working but it diidn’t add two items if they are the same and at least it return something.