Smallest and Largest N | Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Given Number ; Function Call
function findNumberL ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } function findNumberS ( n ) { if ( n == 1 ) return " " ; let result = " " ; let length = Math . floor ( Math . log10 ( n ) + 1 ) ; for ( let i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += ' ' ; } result = n . toString ( ) + result + n . toString ( ) ; return result ; } let N = 3 ; document . write ( " " + findNumberS ( N ) + " " ) ; document . write ( " " + findNumberL ( N ) ) ;