zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Return Largest Numbers in Arrays #294

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

挑战

将每个二维数组中每个元素数组的最大值挑出来,重新组成一个数组,并返回。

代码


function largestOfFour(arr) {
  // You can do this!
  var max=0;
  var arrMax=[];

  for(var i=0;i<arr.length;i++){
    for(var j=0;j<arr[i].length;j++){
      if(arr[i][j]>max){
        max=arr[i][j];
      } 
    }
    arrMax.push(max);
    max=0;
  }

  return arrMax;
}

largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

结果显示

image

帮助

Comparison operators

来源

https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays