Tell us what’s happening:
This is my solution to the inventory update algorithm problem. To me, it looks like the code returns what was asked (an array of items in alphabetical order). However all the tests but one fail. What am I missing here?
Your code so far
function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
function compareWithCurrentInv(newItem, currentInv){
var i = 0;
while( i < curInv.length){
if(newItem[1] === curInv[i][1]){
return [i, newItem[0]];
}
i++;
}
return undefined;
}
for(var i=0; i<arr2.length; i++){
let result = compareWithCurrentInv(arr2[i], arr1)
if(result !== undefined){
arr1[result[0]][0] += result[1]
} else {
arr1.push(arr2[i]);
}
}
arr1.sort((previous, next) =>(
previous[1] > next[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"]
];
updateInventory(curInv, newInv);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
.
Challenge: Inventory Update
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/inventory-update