คำอธิบาย
เขียนฟังก์ชันที่รับอาร์เรย์ของสตริงและคืนค่าสตริงที่มีความยาวสั้นที่สุด
ตัวอย่างโค้ด
const arr = ['this', 'can', 'be', 'some', 'random', 'sentence'];
const findSmallest = arr => {
const creds = arr.reduce((acc, val, index) => {
let { ind, len } = acc;
if (val.length < len) {
len = val.length;
ind = index;
}
return { ind, len };
}, { ind: -1, len: Infinity });
return arr[creds['ind']];
};
console.log(findSmallest(arr));
ผลลัพธ์
be