我在解决以下任务时遇到一些问题:
“提供 max() 函数的完整代码,该函数返回输入数组的最大元素。 array - 整数元素的输入数组”
/*
* @name max - find max element of array
* @return - int - array element
*/
function max(array) {
// Your code here...
}
这是我提供的方法,但它是不正确的。请让我知道我错过了什么。希望得到您的帮助
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));
}
最佳答案
说明说
"Provide complete code of max() function which returns max element of an input array. array - an input array of integer elements"
输入数组听起来像参数 - 它没有提及提示
用户输入。而是在输入参数上调用 Math.max
:
const max = arr => Math.max(...arr);
关于javascript - 返回输入数组的最大元素的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56750256/