เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับตัวเลขและส่งคืนจำนวนตัวเลขที่หารจำนวนอินพุตได้อย่างแม่นยำ
ตัวอย่างเช่น −
หากตัวเลขคือ 12 ตัวประกอบของมันคือ −
1, 2, 3, 4, 6, 12
ดังนั้นผลลัพธ์ควรเป็น 6.
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const num = 12;
const countFactors = num => {
let count = 0;
let flag = 2;
while(flag <= num / 2){
if(num % flag++ !== 0){
continue;
};
count++;
};
return count + 2;
};
console.log(countFactors(num));
console.log(countFactors(2));
console.log(countFactors(454));
console.log(countFactors(99)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
6 2 4 6