-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
34 lines (28 loc) · 1.05 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const input = document.querySelector("textarea");
const wordCount = document.querySelector("[data-word-count]");
const characterCount = document.querySelector("[data-character-count]");
const sentenceCount = document.querySelector("[data-sentence-count]");
const paragraphCount = document.querySelector("[data-paragraph-count]");
input.addEventListener("input", function () {
if (input.value) {
// Count Words
const wordsArray = input.value.split(" ").filter((word) => word !== "");
wordCount.innerText = wordsArray.length;
// Count Characters
characterCount.innerText = input.value.length;
// Count Sentences
const sentenceArray = input.value.split(".");
sentenceCount.innerText = sentenceArray.length - 1;
// Count Paragraph
const paragraphArray = input.value
.split("\n")
.filter((p) => p.trim() !== "");
paragraphCount.innerText = paragraphArray.length;
} else {
wordCount.innerText =
characterCount.innerText =
sentenceCount.innerText =
paragraphCount.innerText =
0;
}
});