动态类型数组中最大数
我们需要编写一个 JavaScript 函数,该函数接受一个包含一些数字、一些字符串和一些 false 值的数组。我们的函数应该从数组中返回最大的数字。
例如:如果输入数组为 -
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];
那么输出应为 65。
因此,让我们编写此函数的代码 -
示例
此代码为 -
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];
const pickBiggest = arr => {
let max = -Infinity;
for(let i = 0; i < arr.length; i++){
if(!+arr[i]){
continue;
};
max = Math.max(max, +arr[i]);
};
return max;
};
console.log(pickBiggest(arr));输出
控制台中的输出将为 -
65
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP