text: Construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | Function that traverses digits in a number and modifies frequency count array ; Array to keep count of digits ; Traversing through x ^ 1 to x ^ n ; For power function , both its parameters are to be in double ; calling countDigits function on x ^ i ; Printing count of digits 0 - 9 ; Driver code
code: function countDigits ( val , arr ) { while ( val > 0 ) { let digit = val % 10 ; arr [ digit ] ++ ; val = Math . floor ( val / 10 ) ; } return ; } function countFrequency ( x , n ) { let freq_count = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { freq_count [ i ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { let val = Math . pow ( x , i ) ; countDigits ( val , freq_count ) ; } for ( let i = 0 ; i <= 9 ; i ++ ) { document . write ( freq_count [ i ] + " " ) ; } } let x = 15 , n = 3 ; countFrequency ( x , n ) ;