เราต้องเขียนฟังก์ชัน JavaScript ที่รับตัวเลขหนึ่งตัวเป็นอาร์กิวเมนต์เดียว ฟังก์ชันต้องนับและคืนค่าจำนวนหลักในตัวเลขที่หารตัวเลขทั้งหมดลงตัว
ตัวอย่าง
ถ้าตัวเลขนำเข้าคือ:
const num = 148;
ผลลัพธ์ควรเป็น:
const output = 2;
เพราะ 148 หารด้วย 1 และ 4 ลงตัว แต่หาร 8 ไม่ลงตัว
ตัวอย่างโค้ด
const num = 148;
const countDividingDigits = (num = 1) => {
let count = 0;
const numStr = String(num);
for(let i = 0; i < numStr.length; i++) {
const curr = +numStr[i];
if(num % curr === 0) {
count++;
}
}
return count;
};
console.log(countDividingDigits(num));
ผลลัพธ์
2