แปลงเศษส่วน p/q (proper fraction) เป็นเศษส่วนผสม (mixed fraction) q r/b โดย q = ผลหาร, r = เศษ, b = ตัวหาร
ตัวอย่าง
// Input: [43, 13] (43/13) // 43 ÷ 13 = 3 เศษ 4 // Output: [3, 4, 13] → "3 wholes and 4/13"
ตัวอย่างโค้ด
const arr = [43, 13];
const properToMixed = arr => {
const quotient = Math.floor(arr[0] / arr[1]);
const remainder = arr[0] % arr[1];
if (remainder === 0) {
return [quotient]; // หารลงตัว
} else {
return [quotient, remainder, arr[1]];
}
};
console.log(properToMixed(arr));
ผลลัพธ์
[ 3, 4, 13 ]
คณิตศาสตร์: 43/13 = 3 + 4/13 = 3 4/13