Handling the Last word separately
NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } document . write ( " " + " " + counter + " " ) ; document . write ( " " + " " + ( NumberOfWords - counter ) + " " ) ; }