function calculateWordCount(text) {
const wordsArr = text.trim().split(" ")
return wordsArr.filter(word => word !== "").length
}
const string = "Hello world"
const charCount = string.length //11
const charCountWithoutWhitespace = string.replace(/s/g, '').length //10
const wordCount = string.split(/s+/).length //2
const paragraphCount = string.replace(/
$/gm, '').split(/
/).length //1
const str = "hello world";
console.log(str.split(' ').length); // @output 2
return str.split(' ').length;
function countWords(str) {
let counts = {};
let words = str.split(' ');
for (let word of words) {
if (word.length > 0) {
if (!(word in counts)) {
counts[word] = 0;
}
counts[word] += 1;
}
}
return counts;
}