Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
function CountWords ( str , k ) { let sum = 0 ; let NumberOfWords = 0 ; let counter = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; ++ i ) { if ( str [ i ] == " " ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } else { sum += str . charCodeAt ( i ) ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } document . write ( " " + " " + counter + " " ) ; document . write ( " " + " " + ( NumberOfWords - counter ) + " " ) ; } let str = " " ; let k = 400 ; CountWords ( str , k ) ;