Largest number smaller than or equal to n and digits in non | Prints the largest number smaller than s and digits in non - decreasing order . ; Array to store digits of number ; Conversion of characters of string var number ; Variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to prvar it ; Driver code
function nondecdigits ( s ) { var m = s . length ; var a = Array . from ( { length : m + 1 } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < m ; i ++ ) a [ i ] = s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; var level = m - 1 ; for ( i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( i = 0 ; i <= level ; i ++ ) document . write ( a [ i ] ) ; for ( i = level + 1 ; i < m ; i ++ ) document . write ( " " ) ; } else { for ( i = 1 ; i < level ; i ++ ) document . write ( a [ i ] ) ; for ( i = level + 1 ; i < m ; i ++ ) document . write ( " " ) ; } } var n = " " ; nondecdigits ( n ) ;