我正在参加CoderCamps,我在第2周。我的最后一个项目是使用HTML和Javascript创建一个Web应用程序,允许用户在textarea中键入一个段落,然后显示每个句子的平均单词,数字网页上的单词,空格和句子。
我的一切工作正常,但我似乎无法得到平均值的总结......任何帮助都会非常感激。
谢谢。
到目前为止,这是我的代码。
var button = document.getElementById("button");
var text = document.getElementById("textarea");
var totalSpaces = 0;
var totalWords = 0;
var avgWords = 0;
var totalSentences = 0;
button.addEventListener("click", function(){
var text = document.getElementById("textarea").value;
text.length
for (var i =0; i<text.length; i++){
if(text[i] == " "){
totalSpaces++;
}
if(text[i] == "."){
totalSentences++;
}
if(text[i] == "?"){
totalSentences++;
}
if(text[i] == "!"){
totalSentences++;
}
}
document.getElementById("avg").innerHTML = totalWords / totalSentences;
document.getElementById("words").innerHTML = totalWords = totalSpaces +1;
document.getElementById("sentences").innerHTML = totalSentences;
document.getElementById("spaces").innerHTML = totalSpaces;
totalSpaces = 0;
totalWords = 0;
avgWords = 0;
totalSentences = 0;
});
答案 0 :(得分:0)
var button = document.getElementById("button");
var text = document.getElementById("textarea");
var totalSpaces = 0;
var totalWords = 0;
var avgWords = 0;
var totalSentences = 0;
button.addEventListener("click", function(){
var text = document.getElementById("textarea").value;
for (var i =0; i<text.length; i++){
if(text[i] == " "){
totalSpaces++;
}
if(text[i] == "."){
totalSentences++;
}
if(text[i] == "?"){
totalSentences++;
}
if(text[i] == "!"){
totalSentences++;
}
}
totalWords = totalSpaces;
document.getElementById("avg").innerHTML = totalWords / totalSentences;
document.getElementById("words").innerHTML = totalWords;
document.getElementById("sentences").innerHTML = totalSentences;
document.getElementById("spaces").innerHTML = totalSpaces;
totalSpaces = 0;
totalWords = 0;
avgWords = 0;
totalSentences = 0;
});
&#13;