Smallest odd digits number not less than N | function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
function smallestNumber ( n ) { var num = 0 ; var s = " " ; var duplicate = n ; while ( n ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 ) ; } var index = - 1 ; for ( var i = 0 ; i < s . length ; i ++ ) { var digit = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; for ( var i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; num = num * 10 + ( s [ index ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; for ( var i = index + 1 ; i < s . length ; i ++ ) num = num * 10 + 1 ; return num ; } var N = 2397 ; document . write ( smallestNumber ( N ) ) ;