I typically use the following code in JavaScript to split a string by whitespace.
\"The quick brown fox jumps over the lazy dog.\".split(/\\s+/);
// [\"The\"
Not elegant as others code but very easy to understand:
countWords(valOf)
{
newArr[];
let str = valOf;
let arr = str.split(" ");
for (let index = 0; index < arr.length; index++)
{
const element = arr[index];
if(element)
{
newArr.push(element);
}
}
const NumberOfWords = newArr.length;
return NumberOfWords;
}