2016-01-11 21 views
2

我正在尝试创建一个javascript函数,用于计算字符串中的字符,单词,空格和平均单词长度,并将它们返回给单个对象。最初,我的人物数量工作,但当加入字数时,我已经迷路了。我可以用其他函数声明一个函数吗?另外,我似乎无法拿到第2个部分的工作,但我不知道什么是错,此代码: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/

+0

你知道的功能当它遇到'return'时停止?在这样的陈述之后有代码是没有意义的。 – usr2564301

+0

你的小提琴的其余部分在哪里? –

回答

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); 
+0

这非常有帮助!非常感谢来自总Noob! –

2

目前已混杂功能和非功能。我认为你的意思是在count中包含计数字,但目前它存在于那里。直接将代码移入count会很麻烦,因为在函数中不能有多个return语句。您将需要跟踪局部变量中的测量结果,然后构造返回值以包含所有值。与此类似,例如:

//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功能将类似于:

var count = function(text) { 
    var charLength = text.length; 
    var wordCount = text.split(' ').length; 
    return { 'charLength': charLength, 'wordCount': wordCount }; 
}; 
+0

非常感谢。我正处于学习的早期阶段,我不确定如何在单一功能中完成多项任务。谢谢! –

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); 

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

+0

非常感谢。 –

0

你需要使这个班看到怎么做此链接:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

一旦你有一个类删除return语句,使全球的变量的类。这是你可以调用类名称,然后拉出显示所需的变量。

您可以通过各种方式获得字数。我个人会使用str.split('').length;如果然后分配这一个变量,你就可以遍历它收集字长统计,拿出你的平均值等

相关问题