To count the number of words in a string, split the string by spaces and count the resulting elements.
- Split the string by spaces into an array of words.
- Filter out any empty strings from the array.
- Return the length of the array.
function countWords(str) {
return str.split(' ').filter(word => word !== '').length;
}
// Example usage
console.log(countWords('Hello world, how are you?')); // Output: 5
console.log(countWords(' This is a test ')); // Output: 4
This method has a time complexity of O(n), where n is the length of the string.
Tags: basic, JavaScript, Strings, Algorithm