Find the number in a range having maximum product of the digits | Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings string a = l . ToString ( ) ; ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code
function product ( x ) { let prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x = parseInt ( x / 10 , 10 ) ; } return prod ; } function findNumber ( l , r ) { let b = r . toString ( ) ; let ans = r ; for ( let i = 0 ; i < b . length ; i ++ ) { if ( b [ i ] == ' ' ) continue ; let curr = b . split ( ' ' ) ; curr [ i ] = String . fromCharCode ( ( ( curr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) - 1 ) + ' ' . charCodeAt ( ) ) ; for ( let j = i + 1 ; j < curr . length ; j ++ ) curr [ j ] = ' ' ; let num = 0 ; for ( let j = 0 ; j < curr . length ; j ++ ) num = num * 10 + ( curr [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } let l = 1 , r = 10 ; document . write ( findNumber ( l , r ) + " " ) ; l = 51 ; r = 62 ; document . write ( findNumber ( l , r ) ) ;