Count of Numbers in Range where first digit is equal to last digit of the number | Javascript program to implement the above approach ; Base Case ; Calculating the last digit ; Calculating the first digit ; Driver code
function solve ( x ) { let ans = 0 , first , last , temp = x ; if ( x < 10 ) return x ; last = x % 10 ; while ( x > 0 ) { first = x % 10 ; x /= 10 ; } if ( first <= last ) ans = 9 + temp / 10 ; else ans = 8 + temp / 10 ; return ans ; } let L = 2 , R = 60 ; document . write ( ( solve ( R ) - solve ( L - 1 ) ) + " " ) ; L = 1 , R = 1000 ; document . write ( solve ( R ) - solve ( L - 1 ) ) ;