ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ตัวเลข ฟังก์ชันของเราควรเลือกและส่งคืนส่วนของตัวเลขหลังจุดทศนิยม (.)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const num = 435.43436;
const retrieveDecimalPart = (num = 1) => {
const str = String(num);
let [_, decimal] = str.split('.');
const divisor = Math.pow(10, decimal.length)
decimal = +decimal;
return decimal / divisor;
};
console.log(retrieveDecimalPart(num)); ผลลัพธ์
ต่อไปนี้เป็นเอาต์พุตคอนโซล -
0.43436