กำหนดจำนวนเต็ม n เราต้องเขียนฟังก์ชันที่คืนค่าจำนวนศูนย์ท้ายใน n!
เช่น:
trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1
ตัวอย่าง
const num = 17;
const findTrailingZeroes = num => {
let cur = 5, total = 0;
while (cur <= num) {
total += Math.floor(num / cur);
cur *= 5;
};
return total;
};
console.log(findTrailingZeroes(num));
console.log(findTrailingZeroes(5));
console.log(findTrailingZeroes(1));
ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็นดังนี้:
3 1 0