function compareSecondColumn(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
let lastObj = []
let newArr = []
let viewArr = []
function updateInventory(arr1, arr2) {
let testArr = [...arr1]
let flat = testArr.flat()
if (arr1.length == 0) {
newArr = [...arr2]
} else {
for (let i = 0; i <= arr2.length - 1; i++) {
for (let j = 0; j <= arr1.length - 1; j++) {
if (arr1[j][1] == arr2[i][1]) {
arr1[j][0] += arr2[i][0]
newArr.push(arr1[j])
break;
} else if (arr1[i][1] != arr2[j][1] && j == arr1.length - 1) {
newArr.push(arr2[i])
}
}
}
let flat2 = newArr.flat()
for (let i = 1; i <= 8; i += 2) {
if (flat.indexOf(flat2[i]) === -1) {
newArr.push([flat[i - 1], flat[i]])
}
}
}
let sorted = newArr.sort(compareSecondColumn)
console.log(sorted)
return sorted;
}
// 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"]
];
console.log(updateInventory(curInv, newInv));
Your code passes for me. Does it pass any of the tests when you Run the Tests?
almost all tests fail on my side except this one
updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])
Comment out the last line (the console.log(updateInventory(curInv, newInv));
). It is interfering with the tests.
Owh thank you…soo much
Since the function mutates the arrays, calling the function with them changes them before the first test starts and all subsequent tests.