I'm trying to add all the numbers for matching arrays and remove the duplicate names. It works for the first instance, but the while loop won't go past Apples.
function updateInventory(arr1, arr2) { function alphabetizer(a, b) { if (a[1] < b[1]) return -1; if (a[1] > b[1]) return 1; return 0; } var newInv = arr1.concat(arr2).sort(alphabetizer); for(i=0;i<newInv.length;i++){ while(newInv[i][1] != -1){ newInv[i][0] += newInv[i+1][0]; newInv.push([newInv[i][0], newInv[i][1]]); newInv.splice(i,2); return newInv; } return newInv; } return newInv; } // Example inventory lists var curInv = [ [21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"], [10, "Apples"] ]; var newInv = [ [9, "Apples"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"] ]; updateInventory(curInv, newInv); So when I run this I just end up getting
[[21,"Bowling Ball"],[67,"Bowling Ball"],[2,"Dirty Sock"],[1,"Hair Pin"],[2,"Hair Pin"],[3,"Half-Eaten Apple"],[5,"Microphone"],[7,"Toothpaste"],[19,"Apples"]]