Find smallest number formed by inverting digits of given number N | Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Given Number ; Function Call
function number ( num ) { let a = new Array ( 20 ) ; let r , j ; let i = 0 ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = parseInt ( num / 10 , 10 ) ; } if ( a [ i - 1 ] == 0 ) { document . write ( 9 ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) document . write ( a [ j ] ) ; } let num = 4545 ; number ( num ) ;