简体   繁体   English

返回输入数组的最大元素的函数

[英]Function which returns max element of an input array

I have some problems with solving the following task: 解决以下任务时遇到一些问题:

"Provide complete code of max() function which returns max element of an input array. array - an input array of integer elements" “提供max()函数的完整代码,该函数返回输入数组的max元素。array-整数元素的输入数组”

/*
* @name max - find max element of array
* @return - int - array element
*/

function max(array) {
  // Your code here...
}

Here is the way I provided, but it is incorrect. 这是我提供的方式,但这是不正确的。 Please let me know what I missed. 请让我知道我错过了什么。 Hope for your help 希望对您有所帮助

function max(array) {
  let numsArr = [];

  while (true) {
    let num = prompt('Enter any number: ', '');

    if (num === '' || num === null) break;

    num = +num;
    numsArr.push(num);
  }

  return alert(Math.max(...numsArr));
}

The instructions say 指示说

"Provide complete code of max() function which returns max element of an input array. array - an input array of integer elements" “提供max()函数的完整代码,该函数返回输入数组的max元素。array-整数元素的输入数组”

The input array sounds like the argument - it says nothing about prompt ing the user for inputs. 输入数组听起来像参数-它没有prompt用户prompt输入。 Call Math.max on the input parameter instead: 在输入参数上调用Math.max代替:

const max = arr => Math.max(...arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示来自 function 的元素,该元素返回 Javascript 中的数组 - How to show element from function which returns array in Javascript 将数组作为输入的javascript函数,将每个数组元素加1,然后返回值增加的新数组不起作用 - javascript function that takes array as input, increases each array element by 1 and returns new array with increased values not working 传递一个返回数组作为参数的函数 - Passing a function which returns array as a parameter Function 仅对数组中的第一个元素返回 true - Function only returns true for first element in array 我用 k 笔交易找到最大利润的函数返回一个空数组 - My function to find the max profit with k transactions returns an empty array dailogflow中数组中的最大元素 - Max element in an array in dailogflow 返回功能的功能 - Function which returns function 在数组数组中交换元素的函数,在特定索引处返回 undefined - Function which is swapping Elements in an Array of Arrays, returns undefined at the specific Indexes Sinon 存根 - 模拟一个返回对象数组的函数 - Sinon stub - mocking a function which returns an array of objects 返回Promises数组的函数的类型应该是什么 - What should be the type of function which returns array of Promises
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM