Phân số thuần (proper fraction): tử < mẫu (dạng p/q).
Hỗn số (mixed fraction): q r/b (q = thương, r = dư).
Viết hàm chuyển [tử, mẫu] → [thương, dư, mẫu].
Ví dụ
[43, 13] → 43 ÷ 13 = 3 dư 4 → [3, 4, 13]
Giải pháp
Mã triển khai:
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];
return [quotient, remainder, arr[1]];
};
console.log(properToMixed(arr));
Kết quả
Đầu ra trong console:
[3, 4, 13]