js学习记录

找出多个数组中的最大数

解法一:loop

function largestOf(arr) {

  constant result=[];

  for(let i = 0 ; i < arr.length; i++){

     let largestNumber=arr[i][0];

     for( let j = 0; j < arr[i].length; j++){

     if(arr[i][j] > largestNumber){

       largestNumber=arr[i][j];

       }

     }
     result[i]=largestNumber;

  }
     return result;
}

解法二:用reduce,map,三目运算符

function largestOf (arr){

   return arr.map(function(group){
          return group.reduce(function(pre,cur){
          return pre>cur?pre:cur;
          });
   });
}

解法三:

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max,null));
}

apply 用于传入数组作为参数,第一个参数为null,map需要回调函数,所以用bind创造一个函数,调用时传入this参数

TL;DR: We build a special callback function (using the Function.bind method), that works just like Math.max but also has Function.prototype.apply's ability to take arrays as its arguments.

  • We start by mapping through the elements inside the main array. Meaning each one of the inner arrays.
  • Now the need a callback function to find the max of each inner array provided by the map.

So we want to create a function that does the work of Math.max and accepts input as an array (which by it doesn’t by default).

In other words, it would be really nice and simple if this worked by itself:

Math.max([9, 43, 20, 6]); // Resulting in 43

Alas, it doesn’t.

  • To do the work of accepting arguments in the shape of an array, there is this Function.prototype.apply method, but it complicates things a bit by invoking the context function.

i.e. Math.max.apply(null, [9, 43, 20, 6]); would invoke something like a Max.max method. What we’re looking for… almost.

Here we’re passing null as the context of the Function.prototype.apply method as Math.max doesn’t need any context.

  • Since arr.map expects a callback function, not just an expression, we create a function out of the previous expression by using the Function.bind method.
  • Since, Function.prototype.apply is a static method of the same Function object, we can call Function.prototype.bind on Function.prototype.apply i.e. Function.prototype.apply.bind.
  • Now we pass the context for the Function.prototype.apply.bind call (in this case we want Math.maxso we can gain its functionality).
  • Since the embedded Function.prototype.apply method will also require a context as it’s 1st argument, we need to pass it a bogus context.
    • So, we pass null as the 2nd param to Function.prototype.apply.bind which gives a context to the Math.max method.

    • Since, Math.max is independent of any context, hence, it ignores the bogus context given by Function.prototype.apply method call.

    • Thus, our Function.prototype.apply.bind(Math.max, null) makes a new function accepting the arr.map values i.e. the inner arrays.

解法四:递归 recursive

function largestOfFour(arr, finalArr = []) {
  return !arr.length
    ? finalArr
    : largestOfFour(arr.slice(1), finalArr.concat(Math.max(...arr[0])))
}

freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays - Guide - The freeCodeCamp Forum

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值