เราต้องเขียนฟังก์ชันที่สร้างอาร์เรย์ 5 ตัวเลขสุ่มไม่ซ้ำกัน ในช่วง [0, 9] และตัวแรกห้ามเป็น 0
ตัวอย่าง
โค้ดต่อไปนี้เป็นตัวอย่าง:
const fiveRandoms = () => {
const arr = []
while (arr.length < 5) {
const random = Math.floor(Math.random() * 10);
if (arr.indexOf(random) > -1){
continue;
};
if(!arr.length && !random){
continue;
}
arr[arr.length] = random;
}
return arr;
};
console.log(fiveRandoms());ผลลัพธ์
จะแสดงผลในคอนโซลดังนี้:
[ 9, 0, 8, 5, 4 ]