库存更新算法挑战

挑战:

参考:库存更新算法挑战

依照一个存着新进货物的二维数组,更新存着现有库存(在 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值