function getQuotientAndRemainder(a, b) {
if (b === 0) {
throw new Error("除数不能为零");
}
// 获取整数商
let quotient = Math.trunc(a / b); // 或者使用 Math.floor(a / b) 或 (a / b) | 0
// 计算余数
let remainder = a % b;
// 判断是否有余数
let hasRemainder = remainder !== 0;
// 返回结果
return {
quotient: quotient,
hasRemainder: hasRemainder
};
}
// 示例调用
let result = getQuotientAndRemainder(10, 3);
console.log(`商是: ${result.quotient}`); // 输出: 商是: 3
console.log(`是否有余数: ${result.hasRemainder}`); // 输出: 是否有余数: true
result = getQuotientAndRemainder(9, 3);
console.log(`商是: ${result.quotient}`); // 输出: 商是: 3
console.log(`是否有余数: ${result.hasRemainder}`); // 输出: 是否有余数: false