Tell us what’s happening:
Hello, first of all, this is my first post here in the forum.
It has been an excellent help since i start studying here in the FCC and i am really thankful for everybody contributing here.
It happens that in this challenge i can’t have the exercise approved even with the correct result.
Is it possible that anyone could help me to understand what am i doing wrong?
Your code so far
function updateInventory(arr1, arr2) {
// Return the index of the item in newInv if is in the curInv
function checkItem (currentList, item){
for (let i=0; i<currentList.length; i++){
if (currentList[i][1] === item) {
return i;
}
}
}
// make a copy of the current inventory
let returnInv = arr1;
// Loop through all the new items to add the returnInv
for (let i=0;i<arr2.length;i++){
var index = checkItem(arr1, arr2[i][1])
// add the quantity of the item if already exists
if (index>=0){
returnInv[index][0]+=arr2[i][0];
}
// add the new item and quantity to the return Inv
else{
returnInv.push(arr2[i])
}
}
//sort alphabetically
return returnInv.sort((a,b)=>a[1]>b[1]);
}
// 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update/