เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ในอาร์เรย์ที่มีตัวเลขบางตัว สตริงบางตัว และค่าเท็จบางส่วน
ฟังก์ชันของเราควรส่งคืนตัวเลขที่ใหญ่ที่สุดจากอาร์เรย์
ตัวอย่างเช่น −
หากอาร์เรย์อินพุตมีค่าต่อไปนี้ซึ่งไม่ได้กำหนดไว้ -
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