Number of sub | Function to return the count of the required substrings ; To store the final answer ; Loop to find the answer ; Condition to update the answer ; Driver code
function countSubStr ( str , len ) { var ans = 0 ; for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) ans += ( i + 1 ) ; } return ans ; } var str = " " ; var len = str . length ; document . write ( countSubStr ( str , len ) ) ;