Largest number not greater than N all the digits of which are odd | Function to check if all digits of a number are odd ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the largest number with all digits odd ; iterate till we find a number with all digits odd ; Driver code
function allOddDigits ( n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 == 0 ) return false ; n = parseInt ( n / 10 ) ; } return true ; } function largestNumber ( n ) { if ( parseInt ( n % 2 ) == 0 ) n -- ; for ( i = n ; i > 0 ; i -= 2 ) if ( allOddDigits ( i ) ) return i ; } var N = 23 ; document . write ( largestNumber ( N ) ) ;