Count number of digits after decimal on dividing a number | JavaScript program to count digits after dot when a number is divided by another . ; let ans = 0 ; Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver code
function count ( x , y ) { let m = new Map ( ) ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . has ( x ) ) return - 1 ; m . set ( x , 1 ) ; x = x * 10 ; } return ans ; } let res = count ( 1 , 2 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ; res = count ( 5 , 3 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ; res = count ( 3 , 5 ) ; if ( ( res == - 1 ) ) document . write ( " " + " " ) ; else document . write ( res + " " ) ;