เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้อาร์เรย์ของตัวเลข จากนั้นฟังก์ชันควรส่งคืนดัชนีของจำนวนที่น้อยที่สุดในอาร์เรย์
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3];
const lowestIndex = arr => {
const creds = arr.reduce((acc, val, ind) => {
let { num, index } = acc;
if(val < num){
num = val;
index = ind;
};
return { num, index };
}, {
num: Infinity,
index: -1
});
return creds.index;
};
console.log(lowestIndex(arr)); ผลลัพธ์
เอาต์พุตในคอนโซล −
6