I am getting the correct values returned but they are formatted as string instead of an array of arrays. How do I use arr.push() to get a 2D array?
My code so far:
function updateInventory(arr1, arr2) {
//pseudocode:
//Get all 2nd element values of first array
//Check each of those against each of the 2nd element values of 2nd array
//If there is NO match, add that element's 1st and 2nd value (from 2nd array) to 1st array
//If there is a match, add to that element's 1st value (in 1st array)
let newArr = [];
let remArr = [];
//Comparing each name in arr1 against all names in arr2. If match, we want to add to total.
for(var i = 0; i < arr1.length; i++){
for(var k = 0; k < arr2.length; k++){
if(arr2[k].indexOf(arr1[i][1]) > -1){
let newTotal = arr1[i][0] + arr2[k][0];
newArr.push(newTotal,arr1[i][1])
}
}
}
let onlyNamesArr2 = [];
for(var n = 0; n < arr2.length; n++){
onlyNamesArr2.push(arr2[n][1]);
}
for(var m = 0; m < arr1.length; m++){
if(onlyNamesArr2.indexOf(arr1[m][1]) === -1){
remArr.push(arr1[m]);
}
}
let onlyNamesArr1 = [];
for(var a = 0; a < arr1.length; a++){
onlyNamesArr1.push(arr1[a][1]);
}
for(var j = 0; j < arr2.length; j++){
if(onlyNamesArr1.indexOf(arr2[j][1]) > -1){
//do nothing
}else {
newArr.push([arr2[j]])
};
}
let finalArr = newArr + remArr;
}
// 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);
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update/