Tell us what’s happening:
It seems that I’ve solved the challenge correctly, but tests 3 and 5 are failing. When I run the code with these test inputs for debugging, they pass but then test 6 starts to fail. After debugging test 6, it passes, but tests 3 and 5 fail again. Could you help me figure out what’s going wrong?
Your code so far
function updateInventory(arr1, arr2) {
// return arr1;
const updatedArray = arr1;
const baseArray = arr1.flatMap( item => {
return item[1]
})
const newInvLength = arr2.length;
const checkInventory = (quantity, item, count) => {
if(baseArray.includes(item)) {
updatedArray.forEach(inventory => {
if(inventory[1] === item) {
const oldValue = inventory[0]
inventory[0] = oldValue + quantity
}
})
} else {
updatedArray.push([quantity, item])
}
}
let count = 0;
while(count < newInvLength) {
checkInventory(newInv[count][0], newInv[count][1])
count++;
}
if(count >= newInvLength) {
const orderedArray = updatedArray.map((inventory) => {
return {qtd: inventory[0], name: inventory[1]}
}).sort((a, b) => {
if(a.name > b.name) {
return 1
}
if (a.name < b.name) {
return -1
}
return 0
}).map((inventory) => {
return [inventory.qtd, inventory.name]
})
console.log({updatedArray, orderedArray})
return orderedArray
}
}
// 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Algorithms - Inventory Update