库存更新算法挑战
function updateInventory(arr1, arr2) {
var arr=[];
outer:for(let x of arr2){
for(let y of arr1){
if(x[1]==y[1]){
y[0]+=x[0];
continue outer;
}
}
arr.push(x);
}
return arr.concat(arr1).sort(function(a,b){
var index=0;
var char_a,char_b;
do{
char_a=a[1].charCodeAt(index);
char_b=b[1].charCodeAt(index);
index++;
}while( char_a==char_b );
return char_a-char_b;
});
}
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);