-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 1.42 KB
/
index.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
35
36
37
38
39
40
41
42
const textInput = document.getElementById("input-text");
document.getElementById("check").addEventListener("click", () => {
const text = textInput.value;
countWords(text);
});
function countWords(text) {
text = text.trim();
const wordsArray = text.split(/\s+/);
const wordCount =
wordsArray.length === 1 && wordsArray[0] === "" ? 0 : wordsArray.length;
const paragraphsArray = text.split(/\n\n|\r\n\r\n/);
const paragraphCount = paragraphsArray.length;
console.log(wordCount);
if (wordCount > 0) {
if (wordCount === 1) {
if (text.length === 1) {
document.getElementById(
"result"
).textContent = `${text.length} character, ${wordCount} word, ${paragraphCount} paragraph`;
} else {
document.getElementById(
"result"
).textContent = `${text.length} characters, ${wordCount} word, ${paragraphCount} paragraph`;
}
} else {
if (paragraphCount === 1) {
document.getElementById(
"result"
).textContent = `${text.length} characters, ${wordCount} words, ${paragraphCount} paragraph`;
} else {
document.getElementById(
"result"
).textContent = `${text.length} characters, ${wordCount} words, ${paragraphCount} paragraphs`;
}
}
} else {
document.getElementById("result").textContent = "";
alert("Paste taste in the text field below.");
}
}