Counting even decimal value substrings in a binary string | function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generate by this character produce even decimal value . ; Driver code
function evenDecimalValue ( str , n ) { let result = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { result += ( n - i ) ; } } return result ; } let str = " " ; let n = 5 ; document . write ( evenDecimalValue ( str , n ) ) ;