Bonfire Inventory Update - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki
Contact me
Gilbert Joseph Smith III
Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail
Details
- Difficulty: 4/5
Compare and update inventory stored in a 2d array against a second 2d array of a fresh delivery. Update current inventory item quantity, and if an item cannot be found, add the new item and quantity into the inventory array in alphabetical order.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
Problem Script:
function inventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
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']
];
inventory(curInv, newInv);
Problem Explanation:
- The program must take two 2D arrays and compare them, if an item from the secodn on is already on the first one then update the first one with the new updated quantity, if it is not then add it to the first one in alphabetical order.
Hint: 1
- You need to handle different cases, the first is to increment the number of stock for items found in both list. Is not hard, just remember how to edit arrays using their index.
Hint: 2
- The second part is to figure out the index of the new inventory that are not in the current inventory. This can be tricky, you can use a function to find it or create copies of the arrays without the numbers to check.
Hint: 3
- The last bit is to sort the array before returning it. It has to be alphabetically so it can be a bit tricky.
Spoiler Alert!
Solution ahead!
Code Solution:
function inventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
var index;
var arrCurInvName = []; // Names of arr1's items
var arrNeInvName = []; // Names of arr2's items
// Same as using two for loops, this takes care of increasing the number of stock quantity.
arr1.map(function(item1) {
return arr2.map(function(item2) {
if (item1[1] === item2[1]) {
item1[0] = item1[0] + item2[0]; //Increase number of stock
}
});
});
// Get item's name for new Inventory
arr2.map(function(item) {
arrNeInvName.push(item[1]);
});
// Get item's name for Current Inventory
arr1.map(function(item) {
arrCurInvName.push(item[1]);
});
// Add new inventory items to current inventory.
arrNeInvName.map(function(item) {
if (arrCurInvName.indexOf(item) === -1) {
index = arrNeInvName.indexOf(item);
arr1.push(arr2[index]);
}
});
// Sort the array alphabetically using the second element of the array as base.
arr1.sort(function(currItem, nextItem) {
//Ternary function to avoid using if else
return currItem[1] > nextItem[1] ? 1 : -1;
});
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']
];
inventory(curInv, newInv);
Code Explanation:
- Read comments in code.