Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-从用户输入的2个数组中查找最大数_Javascript_Multidimensional Array - Fatal编程技术网

javascript-从用户输入的2个数组中查找最大数

javascript-从用户输入的2个数组中查找最大数,javascript,multidimensional-array,Javascript,Multidimensional Array,我花了几个小时试图弄明白这一点,但我不知道我在做什么或不做什么来让这一切顺利进行。感谢您的帮助。我使用以下html从用户处获得2个数字列表: <input type = "text" id = "firstArray"><br/> <input type = "text" id = "secondArray"><br> 然后在我的js函数中,我将2个数字列表附加到一个数组中,使其成为一个数组中的2个数组。我想找到每个数组的最大数,并将这2个数发送

我花了几个小时试图弄明白这一点,但我不知道我在做什么或不做什么来让这一切顺利进行。感谢您的帮助。我使用以下html从用户处获得2个数字列表:

<input type = "text" id = "firstArray"><br/>
<input type = "text" id = "secondArray"><br>
然后在我的js函数中,我将2个数字列表附加到一个数组中,使其成为一个数组中的2个数组。我想找到每个数组的最大数,并将这2个数发送给数组largeList,然后将输出发送到console.log。下面是js函数:

function largestNumbers(arr, arr1) {
  var otherArray = [];
  var largeList = [];
  otherArray[0] = arr.split(" ");
  otherArray[1] = arr1.split(" ");

  console.log("otherArray:  " + otherArray);
  for (var i = 0; i < otherArray.length; i++) {
      var biggestNum = otherArray[i][0];
      for (var j=0; j < otherArray[i].length; j++) {      
          if (otherArray[i][j] > biggestNum) {
              biggestNum = (otherArray[i][j]);
              console.log("big num:  " + biggestNum);

          } 
      }
    largeList[i] = biggestNum;  
  }
  console.log(largeList);
  return largeList; 
}
你可以使用Math.max

可以使用Math.max.apply获取数组中的最大值

函数getResult{ var Answer=largestNumbersdocument.getElementByIdfirstArray.value, document.getElementByIdsecondArray.value; console.logAnswer; 函数最大数arr,arr1{ var largeList=[]; var otherArray=[]; otherArray[0]=arr.split.mapx=>{return parseIntx}; otherArray[1]=arr1.split.mapx=>{return parseIntx}; 调试器 largeList.pushMath.max.applynull,其他数组[0]; largeList.pushMath.max.applynull,otherArray[1]; 回报主义者; } } 获取最大值您可以使用排序、修剪、拆分和贴图功能

例:

函数getLargeFromInputPut{ 返回输入 .value//获取输入值 .trim//从边上删除空格 .split//转换为数组 .mapNumber//将数组项转换为数字 .sortx,y=>y-x//sort DESC [0]//获取最大值 } 函数最大{ var val1=getLargeFromInputdocument.querySelector'inpt1' var val2=getLargeFromInputdocument.querySelector'Input2' var largeList=[val1,val2] 警觉传道者; }
在这里使用Math.max有什么问题?这是家庭作业吗?你可以在这里找到答案。即使你不使用Math.max,这个可能的副本也比它需要的复杂得多。分而治之:给自己一个函数,在单个数组中查找最大的数字。如果愿意,Math.max会这样做:result=Math.max.applyMath,theArray。然后在每个数组上使用该函数两次,每次一次,并返回[firstResult,secondResult]。还可以使用扩展语法Math.max…array来查找数组中的最大值。如果它们不是整数呢。mapNumber会更简单,并且不会做出这样的假设。谢谢你的帮助。这个答案对我来说是最容易合并到原始代码中的。很高兴能帮助你。
function largestNumbers(arr, arr1) {
  var otherArray = [];
  var largeList = [];
  otherArray[0] = arr.split(" ");
  otherArray[1] = arr1.split(" ");

  console.log("otherArray:  " + otherArray);
  for (var i = 0; i < otherArray.length; i++) {
      var biggestNum = otherArray[i][0];
      for (var j=0; j < otherArray[i].length; j++) {      
          if (otherArray[i][j] > biggestNum) {
              biggestNum = (otherArray[i][j]);
              console.log("big num:  " + biggestNum);

          } 
      }
    largeList[i] = biggestNum;  
  }
  console.log(largeList);
  return largeList; 
}
function largestNumbers(arr, arr1) {
  return [Math.max.apply(null, arr), Math.max.apply(null, arr1)];
}