Skip to main content

Posts

Showing posts with the label largest

javascript Return an array consisting of the largest number

 Return an array consisting of the largest number .  题目: 返回一个数组,该数组由每个提供的子数组中的最大数组成。  比如: arraysInArray = [[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]] 我的思路是: 外圈一个大的for循环对大数组中的小数组遍历,内部一个for循环对每个小数组的每个元素进行遍历,用一个变量存储最大的值,用另一个变量记录最大的值所在的小数组在大数组 中的index,最后返回这个arr[index],也就是一个小数组。 function   largestOfFour ( arr ) {    let   largestNum  =  0 ; //存储最大数    let   index  =  0 ; //存储最大数所在的array的序号    for  ( let   i = 0 ; i < arr . length ; i ++) //数组中的数组   {      for ( let   j = 0 ; j < arr [ i ]. length ; j ++) //对每个小数组进行循环判断     {        if  ( largestNum <= arr [ i ][ j ])       {          largestNum  =  arr [ i ][ j ];          index  =  i ; //每次更新index为最大值所在的小数组的index       }     }   }    return   arr [ index ]; } console . log ( largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]])) largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,