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 usinglength
"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 thisvar wordArray = x.split(" ");
variable and then change thiswordAvg += wordArray[i];
to thiswordAvg += 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
4 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - Function that involves averaging word length from a string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745539580a2662430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论