K | Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; Given Input ; Function call
function kthDigitFromLast ( n , k ) { if ( k <= 0 ) { document . write ( - 1 ) ; return ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { document . write ( - 1 ) ; } else { document . write ( parseInt ( n % 10 ) ) ; } } var n = 2354 ; var k = 2 ; kthDigitFromLast ( n , k ) ;