Count digits in given number N which divide N | Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N ; 1 divides all numbers ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver Code
function divisible ( N , digit ) { let ans = 0 ; for ( let i = 0 ; i < N . length ; i ++ ) { ans = ( ans * 10 + ( N [ i ] - ' ' ) ) ; ans %= digit ; } return ( ans == 0 ) ; } function allDigits ( N ) { let divide = [ ] ; for ( let i = 0 ; i < divide . length ; i ++ ) { divide [ i ] = false ; } divide [ 1 ] = true ; for ( let digit = 2 ; digit <= 9 ; digit ++ ) { if ( divisible ( N , digit ) ) divide [ digit ] = true ; } let result = 0 ; for ( let i = 0 ; i < N . length ; i ++ ) { if ( divide [ N [ i ] - ' ' ] == true ) result ++ ; } return result ; } let N = " " ; document . write ( allDigits ( N ) ) ;