Hello everyone
I just finished the Advanced Algorithm Scripting challange “Inventory Update”, and I wanted to get some clarification on some code I used.
For me I had no issue getting the final array, but my issue was sorting a multidimensional array at the end. What I ended up doing was taking the product in array 1, and seeing if its in array 2, then combining the inventories if I did find it. Also I would take it out of the second array… If not then I put that item into a new array. Then I pushed what’s left in array 2 into the new array. After that all I had to do was sort in alphabetical order by product name. Here is my code for reference:
function updateInventory(arr1, arr2) {
//out new arrary
var updatedArr = [];
//loop through our first array and see if the name of the item is in the second array
for(var i = 0; i < arr1.length; i++){
//our item's array that we are teting, the item itself, and the quanity variables
var itemToTestInArr1 = arr1[i];
var arr1InvenItem = arr1[i][1];
var arr1InvenQuanity = arr1[i][0];
//set true or false to false when this loop runs
var trueOrFalse = false;
//now once we selected our item in our first array, loop through our second array to see if it is in there
for(var j = 0; j < arr2.length; j++){
//our second items array that we are testing, the items itself, and the quanity variables
var itemToTestInArr2 = arr2[j];
var arr2InvenItem = arr2[j][1];
var arr2InvenQuanity = arr2[j][0];
//if the item is in there, then do the following
if(itemToTestInArr2.indexOf(arr1InvenItem) === 1) {
//add the quanitys together and set it as the new quanity of the first array
arr1[i][0] = arr1InvenQuanity + arr2InvenQuanity;
//push that array of quanity and item into the new array
updatedArr.push(itemToTestInArr1);
//set true or false to true cause we found it
trueOrFalse = true;
//now remove that array element from our second array
arr2.splice(j, 1);
//if its not found in the second array continue looking through the next items
} else {
continue;
}
}
//if true or false stays false that means we did not find the item in array 2, and we will push it from array 1 into our new array.
if (trueOrFalse === false) {
updatedArr.push(itemToTestInArr1);
}
}
//whatever is left in array 2 means it was not in array 1, so we can just push it into our new array
for (var k = 0; k < arr2.length; k++){
updatedArr.push(arr2[k]);
}
//now we sort the new array by our product name which is index 1 hince the (1) at the end of the entry.
var sortedArr = updatedArr.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(1));
return sortedArr;
}
// 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"]
];
var correctReslut = [
[88, "Bowling Ball"],
[2, "Dirty Sock"],
[3, "Hair Pin"],
[3, "Half-Eaten Apple"],
[5, "Microphone"],
[7, "Toothpaste"]
];
updateInventory(curInv, newInv);
I just need help understanding of the sort functions. I had to search online and found this solution for sorting the nested arrays, but there was no explanation on how it works. Just for reference I am talking about this section:
var sortedArr = updatedArr.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(1));
Also what does the ?
mean in javascript?
Please dumb this down for me a lot because it’s how I learn better, just my personal opinion. I won’t take offense.
Thank you in advance everyone for the help and explanation.