From ee6fb4f29ffe790dd8a33c9d9832b236ae42f74c Mon Sep 17 00:00:00 2001 From: petry Date: Mon, 4 Dec 2023 10:37:31 -0300 Subject: [PATCH] CHAR COUNTER functions now filter blank spaces --- script.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/script.js b/script.js index dd9beaa..6bbb1cf 100644 --- a/script.js +++ b/script.js @@ -37,20 +37,24 @@ function up() {document.getElementById("luzinha").style.color = "#00ff00"} //WORDS / CHARACTERS COUNTERS var inputText = document.getElementById("inputText"); var charCount = document.getElementById("charCount"); +var wordCount = document.getElementById("wordCount"); // assuming you have a word count element -window.addEventListener("load",function(){ +window.addEventListener("load", function(){ var characters = inputText.value.split(''); charCount.innerText = characters.length; - let wordCountString = document.getElementById("inputText").value.split(" ").length - wordCount.innerText = wordCountString + var words = inputText.value.split(/\s+/).filter(function(word) { + return word.length > 0; // filter out empty strings + }); + wordCount.innerText = words.length; }); -inputText.addEventListener("keyup",function(){ +inputText.addEventListener("keyup", function(){ var characters = inputText.value.split(''); charCount.innerText = characters.length; - let wordCountString = document.getElementById("inputText").value.split(" ").length - wordCount.innerText = wordCountString + var words = inputText.value.split(/\s+/).filter(function(word) { + return word.length > 0; // filter out empty strings + }); + wordCount.innerText = words.length; }); -//END \ No newline at end of file