Skip to content

Instantly share code, notes, and snippets.

@aldraco
Created March 1, 2015 18:03
Show Gist options
  • Save aldraco/1a849c294f1ff1151a93 to your computer and use it in GitHub Desktop.
Save aldraco/1a849c294f1ff1151a93 to your computer and use it in GitHub Desktop.
Inventory Update WIP
function inventory(warehouse, shipment) {
// create a key list of items already in warehouse
function makeInvList(list) {
var IL = [];
list.forEach(function(item, index) {
IL.push(item[1]);
});
return IL;
}
var inventoryList = makeInvList(warehouse);
// go through new items
shipment.forEach(function(item, index) {
// check the shipment item against warehouse
// if there is already inventory of that item...
var warehouseRef = inventoryList.indexOf(item[1]);
if (inventoryList.indexOf(item[1]) >= 0) {
// update the count in warehouse.
var shipNum = item[0];
warehouse[warehouseRef][0] += shipNum;
} else {
// else we just push the new inventory item.
warehouse.push(item);
}
});
// now to alphabetize the warehouse.
// create a new inventory list, again, and sort it.
inventoryList = makeInvList(warehouse).sort();
var finalCount = [];
inventoryList.forEach(function(item, value) {
// find the item in warehouse that matches
warehouse.indexOf()
});
return finalCount;
}
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment