我正在尝试做一个句子计数器,而我已经差不多完成了。我做了一个,但我有一个问题,每当有多个句点,问号或感叹号时,它们似乎也算在内

html

 <input id="counter" type="text" />
<p id="sntnceCounter">Sentence(s):</p>


js

function myFunction() {
  var counter = document.getElementById("counter").value;
  var sentences = document.getElementById("sntnceCounter");

 for (var i = 0; i < counter.length; i++) {
    var currentCharacter = counter.substr(i, 1);
    var nextCharacter = counter.substr(i + 1, 1);
 if (
      (currentCharacter == "." ||
        currentCharacter == "?" ||
        currentCharacter == "!") &&
      (nextCharacter !== "." || nextCharacter !== "?" || nextCharacter !== "!")
    ) {
      console.log((sntnceCount += 1));
    }
  }

  sentences.innerHTML = "Sentence(s):" + sntnceCount;
}


通过检查当前字符是句号,问号还是感叹号,并确保如果存在一个字符,它也不会计算下一个字符,从而使句子计数器变大。但是它仍然很重要。我该如何解决?

对不起英语不好

最佳答案

你应该改变

(nextCharacter !== "." || nextCharacter !== "?" || nextCharacter !== "!")




(nextCharacter !== "." && nextCharacter !== "?" && nextCharacter !== "!")

10-06 01:14