Smallest even digits number not less than N | function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code
function trickyCase ( s , index ) { let index1 = - 1 ; for ( let i = index - 1 ; i >= 0 ; i -- ) { let digit = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( digit != 8 ) { index1 = i ; break ; } } if ( index1 == - 1 ) return 2 * Math . pow ( 10 , s . length ) ; let num = 0 ; for ( let i = 0 ; i < index1 ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( s [ index1 ] . charCodeAt ( ) % 2 == 0 ) num = num * 10 + ( s [ index1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 2 ) ; else num = num * 10 + ( s [ index1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ) ; for ( let i = index1 + 1 ; i < s . length ; i ++ ) num = num * 10 ; return num ; } function smallestNumber ( n ) { let num = 0 ; let s = " " ; let duplicate = n ; while ( n > 0 ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 , 10 ) ; } let index = - 1 ; for ( let i = 0 ; i < s . length ; i ++ ) { let digit = s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; let val = digit & 1 ; if ( val == 1 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; if ( s [ index ] == ' ' ) { num = trickyCase ( s , index ) ; return num ; } for ( let i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; num = num * 10 + ( s [ index ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ) ; for ( let i = index + 1 ; i < s . length ; i ++ ) num = num * 10 ; return num ; } let N = 2397 ; document . write ( smallestNumber ( N ) ) ;