Count occurrences of a character in a repeated string | Function to count the character ' a ' ; atleast k repetition are required ; if n is not the multiple of the string size check for the remaining repeating character . ; Driver code
function countChar ( str , x ) { let count = 0 ; let n = 10 ; for ( let i = 0 ; i < str . length ; i ++ ) if ( str [ i ] == x ) count ++ ; let repetitions = n / str . length ; count = count * repetitions ; for ( let i = 0 ; i < n % str . length ; i ++ ) { if ( str [ i ] == x ) count ++ ; } return count ; } let str = " " ; document . write ( countChar ( str , ' ' ) ) ;