Javascript - How to take a string, print the # of words, characters, spaces, avg word length and return as an object?
<p>I'd like to pass the function a string and print the number of 
words, characters, spaces, and the average length of 
each word as object properties. I'm looking for the return to be </p>

<p>{"numWords": 7, "numCharacters": 30, "numSpaces": 6, "avgWordLength": 3.x}</p>

<p>I'm also really new, so I'll take any advice on anything that looks wrong or could be better. Thanks!</p>

<pre><code>function allTheCounts(str) {

  var numWords = str.split(" ").length; 
  var numCharacters = str.length; 
  var numSpaces = numWords - 1;
  var strArray = str.split(" ");
  var avgWordLength = 0;    
    for (var i = 0; i &lt; numWords.length; i++){
    avgWordLength += strArray[i].length;
    }
  var avgWordLength = avgWordLength/numWords;

  var answerObject = {
    numWords: numWords,
    numCharacters: numCharacters,
    numSpaces: numSpaces,
    avgWordLength: avgWordLength
  };
 return answerObject;
}
 allTheCounts("Hello world. It is a good day.");
</code></pre>