-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
54 lines (42 loc) · 1.53 KB
/
Copy pathscript.js
File metadata and controls
54 lines (42 loc) · 1.53 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
// Get word + charcounter from textArea 2.0
let input = document.getElementById("notepad"),
words = document.getElementById('words'),
characters = document.getElementById('characters');
// A string of characters that count towards your word / character count.
// Includes a-z, A-Z, and 0-9.
const charactersThatCount = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLNOPQRSTUVWYXZ0123456789";
function countWords() {
var text = input.value;
var wordCount = 0;
var isWord = false;
// Removed the "=" in "<=" to prevent an extra word from being counted.
for (var i = 0; i < text.length; i++) {
const letter = text.charAt(i);
if (charactersThatCount.includes(letter)) {
isWord = true;
} else {
// This only gets fired if the previous letter is in "charactersThatCount"
if (isWord) wordCount++;
isWord = false;
}
}
// If your input ends in a letter, count it as a word. (E.G. "Two words")
if (isWord) wordCount++;
words.innerText = wordCount;
}
function countCharacters() {
var text = input.value;
var letterCount = 0;
for (var i = 0; i <= text.length - 1; i++) {
const letter = text.charAt(i);
// If the letter is in "charactersThatCount", then add a letter.
if (charactersThatCount.includes(letter)) letterCount++;
}
characters.innerText = letterCount;
}
// Merged both event listeners into one.
input.addEventListener('keyup', function (e) {
// Removed "e.value", as "var text = input.value;" overwrites it.
countWords();
countCharacters();
});