[Solved] Use JavaScript to count words in a string, WITHOUT using a regex


So, here is the answer to your new question:

My overall goal is to find the shortest word in the string.

It splits the string as before, sorts the array from short to long words and returns the first entry / shortest word:

function WordCount(str) { 
    var words = str.split(" ");

    var sortedWords = words.sort(function(a,b) {
        if(a.length<b.length) return -1;
        else if(a.length>b.length) return 1;
        else return 0;
    });
   return sortedWords[0];
}

console.log(WordCount("hello to the world"));

1

solved Use JavaScript to count words in a string, WITHOUT using a regex