Help with the character counter

Hi guys, I’m having trouble with a .js for a character and word counter… it turns out that it’s considering punctuation and spaces as characters. Does anyone know how I can edit this code so that it ignores punctuation and spaces?

// Get word + charcounter from textArea 2.0
let input = document.getElementById(“notepad”),
words = document.getElementById(‘words’),
characters = document.getElementById(‘characters’);
function wordCounter(text) {
var text = input.value;
var wordCount = 0;
for (var i = 0; i <= text.length; i++) {
if (text.charAt(i) == ’ ') {
wordCount++;
}
}
words.innerText = wordCount;
}
input.addEventListener(‘keyup’, function(e){
wordCounter(e.value);
});

function caracterCounter(text) {
var text = input.value;
var wordCount = 0;
for (var i = 0; i <= text.length - 1; i++) {
text.charAt(i) == wordCount++;
}
characters.innerText = wordCount;
}
input.addEventListener(‘keyup’, function(e){
caracterCounter(e.value);
});

let’s start by taking inventory of what you have. can you post a summary of how your wordCounter function and caracterCounter [sic] function differ in their operation?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.