2012-10-13 27 views
0
<!DOCTYPE HTML> 

<html> 
<head> 
    <title> Javascript - stuff </title> 
    <script type="text/javascript"> 
    <!-- 
    function GetCountsAll(Wordcount, Sentancecount, Clausecount, Charactercount) 
    { 
     var TextString = document.getElementById("Text").innerHTML; 
     var Wordcount = 0; 
     var Sentancecount = 0; 
     var Clausecount = 0; 
     var Charactercount = 0; 

     // For loop that runs through all characters incrementing the variable(s) value each iteration 
     for (i=0; i < TextString.length; i++); 
     if (TextString.charAt(i) == " " = true) 
      Wordcount++; 
     return Wordcount; 

     if (TextString.charAt(i) = "." = true) 
      Sentancecount++;      
     Clausecount++; 
     return Sentancecount; 

     if (TextString.charAt(i) = ";" = true) 
     Clausecount++; 
     return Clausecount; 
    } 


    --> 
    </script> 
</head> 

<body> 

    <div id="Text"> 
     It is important to remember that XHTML is a markup language; it is not a programming language. The language only describes the placement and visual appearance of elements arranged on a page; it does not permit users to manipulate these elements to change their placement or appearance, or to perform any "processing" on the text or graphics to change their content in response to user needs. For many Web pages this lack of processing capability is not a great drawback; the pages are simply displays of static, unchanging, information for which no manipulation by the user is required. Still, there are cases where the ability to respond to user actions and the availability of processing methods can be a great asset. This is where JavaScript enters the picture. 
    </div> 
    <input type = "button" value = "Get Counts" class = "btnstyle" onclick = "GetCountsAll()"/> 
    <br/>  
    <span id= "Charactercount"> </span> Characters <br/> 
    <span id= "Wordcount"> </span> Words <br/> 
    <span id= "Sentancecount"> </span> Sentences <br/> 
    <span id= "ClauseCount"> </span> Clauses <br/> 

</body> 
</html> 

我是一名学生,仍在学习JavaScript,所以请原谅任何可怕的错误。该脚本旨在计算该段落中的字符,单词,句子和条款的数量。简单地说,就是不工作。我已经尝试了很多事情来让它为我工作,并且得到了很多不同的错误,但是不管我无法得到这个工作。请帮忙! (顺便说一句,我知道我拼写错误的句子)用于统计字符/单词不工作的功能

+3

为什么在for语句后面有';'?而且你不计算单词 - 你正在计算字符串中的空格数量,这可能不是实际的单词数量。我对代码进行了重新缩进,以使问题更加混杂 – nhahtdh

+0

此代码是由您编写的吗? – zerkms

+0

我知道空间可能不是==这个词确切地说,这是可以接受的。 – user1742729

回答

0

for (i=0; i < TextString.length; i++);删除分号。这打破了循环。

放置括号{}大约
Sentancecount++;
Clausecount++;
所以它们在每次看到句号时递增。目前只有Sentance每次增加。子句在文本的末尾增加。

我也会在if之后使用括号。它提高了可读性,如果您可以轻松阅读代码,您可以看到代码的功能。

接下来,您只能希望从该方法返回一个。如果有意义的话,让第一种方法调用次要方法。设置它,让你得到一些变量被赋予的值,然后打印出来。

心连心

+0

谢谢,这让我觉得有点接近。 – user1742729

+0

显然我不能upvote或youd都有一个,谢谢你的指导。 – user1742729

0

你可以使用字符串的方法split。就像这样:

WordCount = TextString.split(' ').length; 
Sentancecount = TextString.split('.').length; 
Clausecount = TextString.split(';').length; 
Charactercount = TextString.length; 

这样,你不需要for循环了。

+0

谢谢,生病阅读该方法 – user1742729

+0

我的意思是,有更多的帮助,你需要你的问题吗? –

0

那么,你在这里有几个问题。

首先,您希望您的for循环在您想要在每次迭代中执行的代码周围加上大括号。

for (i=0; i < TextString.length; i++) { 
    if (TextString.charAt(i) == " " = true) 
     Wordcount++; 
} 

(我假设字计数应该是外循环(但它是不正确任何一种方式))

接下来,你不会处理这些返回值。

工作方式return的工作原理是可以为函数的结果设置一个变量。

例如: 函数x(){ return 2;所以 }

var i = 1; 
var q = i + x(); // Add the result of the function to i 
alert(q); // This will display 3 

致电回报,你发送的是值回哪里调用函数,但你永远不会使用它。

此外,调用return立即退出该功能。如果你想继续处理,你不需要这个。我建议你为三个值中的每一个创建变量并将结果存储到该值。然后在脚本中处理之后,可以将div的innerHTML指向它们。

下面是一个例子,可以帮助你:

<html> 
<head> 
    <title>Example Javascript</title> 
    <script type="text/javascript"> 

    function myFunction() 
    { 
     var text = Array(8).join(parseInt('')) + ' Batman!'; 
     var element = document.getElementById("result"); 

     element.innerHTML = text; 
    } 

    </script> 
</head> 
<body> 
The output from our function is: <div id="result"> </div> 
<br /> 
<button type="button" onclick="myFunction()">Run</button> 
</body> 
</html> 

我想指出,有一些非常简单的方法来解决这个问题,但是这似乎是功课或至少一个学习的经验,这就是为什么我让你走上正确的道路,而不仅仅是给你一个直接的答案。你很近!我的建议是坚持下去!

相关问题