[英]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.