Skip to content

Javascript course - Word Analytics word count bug #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gizmola opened this issue Nov 5, 2024 · 0 comments
Open

Javascript course - Word Analytics word count bug #11

gizmola opened this issue Nov 5, 2024 · 0 comments

Comments

@gizmola
Copy link

gizmola commented Nov 5, 2024

This might be a somewhat pedantic issue, but working through the javascript course, I wrote my own code first, then comparedd it to yours and found a couple of pretty obvious bugs with the word count feature.

As someone who has bought all your courses, let me say in advance that I really enjoy your lecture style and command of the material, so this is really just something for those who are trying to dig beneath the surface level of the projects, or trying out variations using their own code.

Since the word count splits on a space constant, the number of words will be off if:

  • any whitespace is added, so in your video even though you have 4 words, the word count is 5 as you type once you add an extra space.
  • adding some whitespace before or after throws off the word count and the check for an empty entry
  • with the space constant ' ', split doesn't understand other whitespace characters like newlines and tabs

This can of course be fixed fairly easily with some simple regex and slight variation to your code (my variable names might be different from yours):

const numwordsEl = document.querySelector(".stat__number--words");

In the input handler function...

if (textareaEl.value.trim().length > 0) {
    const strWords = textareaEl.value.trim().split(/\s+/).length;
    numwordsEl.textContent = strWords;
} else {
    numwordsEl.textContent = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant