javascript字符,单词和&空间计数功能

时间:2016-01-11 23:15:38

标签: javascript string function

我正在尝试创建一个javascript函数,用于计算字符串中的字符,单词,空格和平均字长,并将它们返回到单个对象中。最初我有字符计数工作,但添加字数,我已经迷路了。我可以用其他函数声明一个函数来执行此操作吗?此外,我似乎无法使前两部分正常工作,但我不确定此代码有什么问题:

var charLength = 0;
var count = function(text) {
  var charLength = text.length;
  return charLength;
};
var wordCount = 0;
for (i = 1; i < text.length; i++) {
  if (text.charAt(i) == " ") {
    wordCount++;
  }
  return wordCount + 1;
  console.log("Characters: " + charLength + " Words: " + wordCount);
}
var text = "Hello there fine sir.";
count(text);

这是jsFiddle:https://jsfiddle.net/minditorrey/z9nwhrga/1/

4 个答案:

答案 0 :(得分:2)

目前,您有混合功能和非功能。我认为你的意思是在from PyQt4 import QtCore, QtGui class MyWidget(QtGui.QWidget): testSignal = QtCore.pyqtSignal(object) def __init__(self, parent=None) super(MyWidget, self).__init__(parent) self.testSignal.connect(self.testSlot) def testSlot(self, message): print(message) def mouseReleaseEvent(self, event): self.testSignal.emit('mouse release') super(MyWidget, self).mouseReleaseEvent(event) 内包含单词count,但目前它存在于那里之外。然而,将该代码直接移动到count将会很麻烦,因为在函数内部不能有多个return语句。您需要跟踪局部变量中的测量值,然后构造一些包含所有值的返回值。像这样,例如:

count

为了更进一步,您可以将单词计数简化为:

//var charLength = 0; You have a global and local variable, omit this one
var count = function(text) {
  var charLength = text.length;
  var wordCount = 0;
  for (var i = 0; i < text.length; i++) { // declare i with var, start at 0
    if (text.charAt(i) == " ") {
      wordCount++;
    }
  }
  return { 'charLength': charLength, 'wordCount': wordCount };
};

var text = "Hello there fine sir.";
console.log(count(text)); // add a way to see the results

所以你的新text.split(' ').length 函数看起来像是:

count

答案 1 :(得分:0)

检查这个(或在jsbin:https://jsbin.com/tazayaqige/edit?js,console中):

var count = function(text) {
  var charLength = text.length;
  var wordCount = text.split(" ").length;

  console.log("Characters: " + charLength + " Words: " + wordCount);
  return { wordCount: wordCount, charLength: charLength };
}

var text = "Hello there fine sir.";
count(text);

您正在尝试定义两个函数,但实际上您定义了count函数,但另一个wordCount未定义为函数。只是声明,并且有一个return语句等。无论如何,检查上面的代码,它应该做的伎俩。

答案 2 :(得分:0)

您需要将此作为课程,请参阅此链接,了解如何执行此操作:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

一旦你有一个类丢弃return语句并使变量全局变为类。这是你可以调用类名,然后拉出显示所需的变量。

您可以通过各种方式获取字数。我个人会使用str.split('')。length;如果你把它分配给一个变量你可以循环它收集字长的统计数据并得出你的平均值等。

答案 3 :(得分:0)

我更新了您的fiddle

function goforit(text){
  var charLength = text.length;

  var spacesCount = 0;
  for (i = 1; i < text.length; i++) {
    if (text.charAt(i) == " ") {
      spacesCount++;

    }

  }
  var wordsArray = text.split(" ");
  var totalWordLen=0;
  for (j = 0; j < wordsArray.length; j++) {
    totalWordLen = totalWordLen + wordsArray[j].length;

  }  
  var wordCount = spacesCount+1;
  var average = totalWordLen / wordsArray.length;
  var outputString = "Characters: " + charLength + " Words: " + wordCount + " Spaces: " + spacesCount + " Average: " + average;
    alert(outputString)
  console.log(outputString);
}



var text = "Hello there fine sir.";
goforit(text);