Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | Function to perform every step ; Perform N steps ; Traverse in the string ; For last digit ; Recur for current string ; Driver code
function countDigits ( st , n ) { if ( n > 0 ) { let cnt = 1 , i ; let st2 = " " ; for ( i = 1 ; i < st . length ; i ++ ) { if ( st [ i ] == st [ i - 1 ] ) cnt ++ ; else { st2 += String . fromCharCode ( ' ' . charCodeAt ( ) + cnt ) ; st2 += st [ i - 1 ] ; cnt = 1 ; } } st2 += String . fromCharCode ( ' ' . charCodeAt ( ) + cnt ) ; st2 += st [ i - 1 ] ; countDigits ( st2 , -- n ) ; } else document . write ( st ) ; } let num = " " ; let n = 3 ; countDigits ( num , n ) ;