用加减乘实现除法

朋友给的练习题,本以为简单,但不成想需要考虑的条件多多:

function divider(dividend,divisor,decimal=0){
     if(divisor == 0){//除数不能为0
       throw new Error('divisor should not be 0')
     }
     if(decimal < 0 || parseInt(decimal) != decimal){//精确度只能为正整数
       throw new Error('decimal should be a positive int')
     }
     //判断最终结果是负数还是正数
     let isResultPositive = true
     if(dividend * divisor > 0){
       isResultPositive = true
     }else{
       isResultPositive = false
     }
     dividend = Math.abs(dividend)
     divisor = Math.abs(divisor)
     //整数部分的除法
      function intDivider(dividend,divisor){
        let i = 0
        for(i; dividend - (divisor * i) >= divisor; i++){
        }
        let quoten = i
        return quoten
      }
      //10的阶乘,为小数部分的除法做准备
      function power10 (pow){
        let num = 1
        for(let i=0;i

你可能感兴趣的:(用加减乘实现除法)