Skip to content

Commit

Permalink
Improve logic chars
Browse files Browse the repository at this point in the history
  • Loading branch information
glauberramos committed Feb 19, 2024
1 parent 6a200c8 commit 0a8fa20
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
function countWords(str) {
const words = str.trim().split(/\s+/).length;
const chars = str
.match(/./gu)
.map((char) => char.trim())
const words = str
.trim()
.split(/\s+/)
.filter((char) => char).length;
document.getElementById("words").innerText =
words + " words " + chars + " chars";

const allChars = str.match(/./gu);
const chars =
allChars &&
allChars.map((char) => char.trim()).filter((char) => char).length;

document.getElementById("words").innerText = `${words} word ${
chars ? chars : 0
} chars`;
}

document.addEventListener("DOMContentLoaded", function () {
Expand Down

0 comments on commit 0a8fa20

Please sign in to comment.