admin管理员组

文章数量:1429147

This is my first time ever touching JavaScript, and I am writing a function that will output the following:

  • Individual words in the string,
  • Total character count,
  • Total word count,
  • Whitespace count,
  • and finally the average word length.

So far I have all aspects plete except I am struggling with the averaging process.

My code:

var superCounter = function(x) {
  var charCount = x.length;
  var wordCount = x.split(" ").length;
  var whiteSpace = wordCount - 1; 
  var wordArray = [x.split(" ")];
  var wordAvg = 0;
  for (var i = 0; i < wordCount.length; i++){
    wordAvg += wordArray[i];
  }
  var avgLen = wordAvg / wordCount;
  console.log(("Words: " + wordArray[0]), "Character count: " + charCount, "Word count: " + wordCount, "Whitespace count: " + whiteSpace, "Word length average: " + avgLen);
};

superCounter("This function will analyze strings");

I like everything as it is except the averaging.

Output:

"Words: This,function,will,analyze,strings"
"Character count: 34"
"Word count: 5"
"Whitespace count: 4"
"Word length average: 0"

I know I somehow need the length of i, but every way I have attempted to apply length hasn't worked.

Ideas?

Thank you!

This is my first time ever touching JavaScript, and I am writing a function that will output the following:

  • Individual words in the string,
  • Total character count,
  • Total word count,
  • Whitespace count,
  • and finally the average word length.

So far I have all aspects plete except I am struggling with the averaging process.

My code:

var superCounter = function(x) {
  var charCount = x.length;
  var wordCount = x.split(" ").length;
  var whiteSpace = wordCount - 1; 
  var wordArray = [x.split(" ")];
  var wordAvg = 0;
  for (var i = 0; i < wordCount.length; i++){
    wordAvg += wordArray[i];
  }
  var avgLen = wordAvg / wordCount;
  console.log(("Words: " + wordArray[0]), "Character count: " + charCount, "Word count: " + wordCount, "Whitespace count: " + whiteSpace, "Word length average: " + avgLen);
};

superCounter("This function will analyze strings");

I like everything as it is except the averaging.

Output:

"Words: This,function,will,analyze,strings"
"Character count: 34"
"Word count: 5"
"Whitespace count: 4"
"Word length average: 0"

I know I somehow need the length of i, but every way I have attempted to apply length hasn't worked.

Ideas?

Thank you!

Share Improve this question edited Jun 22, 2015 at 17:32 Dave Newton 160k27 gold badges261 silver badges308 bronze badges asked Jun 22, 2015 at 17:30 Charles WatsonCharles Watson 1,0752 gold badges16 silver badges39 bronze badges 4
  • i is a number. It doesn't have a length. Do you mean the length of that element of the array? What did you try? – SLaks Commented Jun 22, 2015 at 17:32
  • 1 You add words (strings) to wordAvg (a number). Saying that using length "didn't work" won't help anybody diagnose, what specifically happened? What did you try? – Dave Newton Commented Jun 22, 2015 at 17:34
  • Change this var wordArray = [x.split(" ")]; to this var wordArray = x.split(" "); variable and then change this wordAvg += wordArray[i]; to this wordAvg += wordArray[i].length; – user1106925 Commented Jun 22, 2015 at 17:37
  • It's the total number of characters divided by number of words, like this var avgLen = wordArray.join('').length / wordCount -> jsfiddle/mo9Lqrkr – adeneo Commented Jun 22, 2015 at 17:39
Add a ment  | 

4 Answers 4

Reset to default 5

You almost had it, just a couple small changes and your code works great.

wordArray should be declared as follows:

var wordArray = x.split(" ");

Then you need to correct the summation of the word character lengths to wordAvg.

Change this:

wordAvg += wordArray[i];

To this:

wordAvg += wordArray[i].length;
 wordAvg += wordArray[i];

should be

 wordAvg += wordArray[i].length;

Should be something like this:

var superCounter = function(x) {
    var charCount = x.length;
    var wordCount = x.split(" ").length;
    var whiteSpace = wordCount - 1; 
    var wordArray = x.split(" ");
    var wordAvg = 0;
    for (var i = 0; i < wordCount; i++){
        wordAvg += wordArray[i].length;
    }
    var avgLen = wordAvg / wordCount;
    console.log(("Words: " + wordArray), "Character count: " + charCount, "Word count: " + wordCount, "Whitespace count: " + whiteSpace, "Word length average: " + avgLen);
};

superCounter("This function will analyze strings");

Maybe try something like this :

 function jCounter(data) { 
     var strArr = data.split(' ');
     var strChar = data.length;
     var objStr = strArr.length;   
     var strSpaces = objStr - 1;

     var numArr = [];
     for (i=0; i<objStr; i++) {
        numArr.push(strArr[i].length); 
     }

     var sum = 0;
     for (i=0; i<numArr.length; i++) {
        sum = sum + numArr[i];
     } 

     var avg = sum/objStr;
     var strObj = {}; 

     strObj.words = objStr;
     strObj.chars = strChar;
     strObj.avgLength = avg ;
     strObj.spaces = strSpaces;

     console.log(strObj);
   } 

   jCounter("Coding is challenging");

本文标签: javascriptFunction that involves averaging word length from a stringStack Overflow