I made a simple site with a little js logic to count the number of sentences, words, and characters in text content. You can get the output by typing or pasting your text content. And I need help with some code review. I don’t know if there is a section here dedicated to that.
https://wordcounter.aabraham.dev < deployed here
GitHub - abelabrahamx/wordcounter < github.
for convenience, I’ll post the script here. I want to know if there is an alternative to the onmousemove
event listener when the client paste’s the content.
const textInput = document.querySelector('#content');
const word = document.querySelector('#word');
const character = document.querySelector('#character');
const sentence = document.querySelector('#sentence');
const counter = () => {
let wordList = textInput.value.split(' ');
let characterList = textInput.value.split('')
let sentenceList = textInput.value.split('.')
let wordCount = 0;
let characterCount = 0
let sentenceCount = 0
for (let i = 0; i < wordList.length; i++) {
if (wordList[i] !== '') {
wordCount++
}
}
for (let i = 0; i < characterList.length; i++) {
if (characterList[i] !== ' ' && characterList[i] !== '.' && characterList[i] !== ',') {
characterCount++
}
}
for (let i = 0; i < sentenceList.length; i++) {
if (sentenceList[i] !== '') {
sentenceCount++
}
}
word.innerHTML = wordCount;
character.innerHTML = characterCount;
sentence.innerHTML = sentenceCount;
}
textInput.onkeydown = counter;
document.querySelector('body').onpointermove = counter;