// 实现一个函数模拟除法,用括号把无限循环小数扩起来,
// 例如 1/3 = 1.333333,该函数需要返回'1.(3)'。
// 1/3 返回 1.(3) #重复循环的用括号括起来
function divisionFn(a,b){
let hisMap = new Map();
// 整数部分
let integerPart = Math.floor(a / b)
// 取余数
let remainder = a%b
// 小数部分
let decimalPart = ''
let i = 0
while (remainder) {
if (hisMap.has(remainder)){
let pre = decimalPart.substr(0, hisMap.get(remainder))
// 循环小数部分
let cycle = decimalPart.substr(hisMap.get(remainder))
return `${integerPart}.${pre}(${cycle})`
}else{
let temp = remainder * 10
hisMap.set(remainder, i)
decimalPart += Math.floor(temp / b)
remainder = temp % b
i++
}
}
return decimalPart.length ? `${integerPart}.${decimalPart}` : `${integerPart}`
}
console.log(divisionFn(1,3))
console.log(divisionFn(1, 6))
console.log(divisionFn(1, 2))
console.log(divisionFn(12, 2))
console