库存更新算法挑战

挑战:

参考:库存更新算法挑战

依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的字母顺序排列.

例如:

updateInventory() 应该返回一个数组.

updateInventory(
[[21, “Bowling Ball”], [2, “Dirty Sock”], [1, “Hair Pin”], [5, “Microphone”]],
[[2, “Hair Pin”], [3, “Half-Eaten Apple”], [67, “Bowling Ball”], [7, “Toothpaste”]]
).length
应该返回一个长度为6的数组.

updateInventory(
[[21, “Bowling Ball”], [2, “Dirty Sock”], [1, “Hair Pin”], [5, “Microphone”]],
[[2, “Hair Pin”], [3, “Half-Eaten Apple”], [67, “Bowling Ball”], [7, “Toothpaste”]]
)
应该返回 [[88, “Bowling Ball”], [2, “Dirty Sock”], [3, “Hair Pin”], [3, “Half-Eaten Apple”], [5, “Microphone”], [7, “Toothpaste”]].

updateInventory(
[[21, “Bowling Ball”], [2, “Dirty Sock”], [1, “Hair Pin”], [5, “Microphone”]],
[]
)
应该返回 [[21, “Bowling Ball”], [2, “Dirty Sock”], [1, “Hair Pin”], [5, “Microphone”]].

updateInventory(
[],
[[2, “Hair Pin”], [3, “Half-Eaten Apple”], [67, “Bowling Ball”], [7, “Toothpaste”]]
)
应该返回 [[67, “Bowling Ball”], [2, “Hair Pin”], [3, “Half-Eaten Apple”], [7, “Toothpaste”]].

updateInventory(
[[0, “Bowling Ball”], [0, “Dirty Sock”], [0, “Hair Pin”], [0, “Microphone”]],
[[1, “Hair Pin”], [1, “Half-Eaten Apple”], [1, “Bowling Ball”], [1, “Toothpaste”]]
)
应该返回 [[1, “Bowling Ball”], [0, “Dirty Sock”], [1, “Hair Pin”], [1, “Half-Eaten Apple”], [0, “Microphone”], [1, “Toothpaste”]].

答案:

方法 描述
for…of 创建一个循环来迭代可迭代的对象。
let 声明的变量只在 let 命令所在的代码块内有效。
continue 用于跳过循环中的一个迭代,并继续执行循环中的下一个迭代。
push() 数组的末尾添加一个或多个元素,并返回新的长度。
concat() 用于连接两个或多个数组,返回被连接数组的一个副本。
sort() 对数组的元素进行排序。
charCodeAt() 返回指定位置的字符的 Unicode 编码。
do/while 循环至少执行一次,即便条件为false,因为代码块是在条件语句判断前执行

do{
char_a=a[1].charCodeAt(index);
char_b=b[1].charCodeAt(index);
index++;
while( char_a==char_b )
说明:当 index 大于字符串长度时,返回 NaN 如果 a 和 b 都是 NaN,则结束循环。index++ 就是防止死循环。
字符串中第一个字符的下标是 0。如果 index 是负数,或大于等于字符串的长度,则 charCodeAt() 返回 NaN。
在JavaScript中NaN为什么不等于NaN

function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!
    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);   //arr2独有的放进arr
    }
    return arr.concat(arr1).sort((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;
    });
}
// 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);

运行结果:

[[88,“Bowling Ball”],
[2,“Dirty Sock”],
[3,“Hair Pin”],
[3,“Half-Eaten Apple”],
[5,“Microphone”],
[7,“Toothpaste”]]

在线测试:

库存更新算法挑战 | w3cschool

发布了56 篇原创文章 · 获赞 1 · 访问量 834

猜你喜欢

转载自blog.csdn.net/weixin_44790207/article/details/104806405