Count the number of occurrences of a particular digit in a number | Function to count the occurrences of the digit D in N ; Loop to find the digits of N ; check if the digit is D ; return the count of the occurrences of D in N ; Driver code
function countOccurrances ( n , d ) { let count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = parseInt ( n / 10 ) ; } return count ; } let d = 2 ; let n = 214215421 ; document . write ( countOccurrances ( n , d ) ) ;