Why does This solution for Inventory Update from the interview section doesnt pass the test

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));

almost all tests fail on my side except this one

updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])

Owh thank you…soo much

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.