-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
69 lines (63 loc) · 1.89 KB
/
popup.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var wordInput = document.getElementById("word-input");
function getCharacterCount() {
var characterCount = document.getElementById("character-count");
if (wordInput.value.length > 0) {
characterCount.innerHTML = wordInput.value.trim().split("").length
} else {
characterCount.innerHTML = 0
}
}
function getWordCount() {
var wordCount = document.getElementById("word-count");
if (wordInput.value.length > 0) {
wordCount.innerHTML = wordInput.value.trim().split(" ").length
} else {
wordCount.innerHTML = 0
}
}
function getSentenceCount() {
var sentenceCount = document.getElementById("sentence-count");
var wordArray = wordInput.value.split(" ");
var punctuation = [".", "!", "?"];
var sentenceArray = wordArray.filter(x => punctuation.includes(x[x.length-1]));
if (wordInput.value.length > 0) {
sentenceCount.innerHTML = sentenceArray.length
} else {
sentenceCount.innerHTML = 0
}
}
function getMostUsedWord() {
var mostUsedWord = document.getElementById("most-used-word");
var wordArray = wordInput.value.split(" ");
var initialCount = 1;
var count = 0;
for (var i=0; i<wordArray.length; i++)
{
for (var j=i; j<wordArray.length; j++)
{
if (wordArray[i] == wordArray[j]) {
count++;
if (initialCount<count){
initialCount = count;
var word = wordArray[i];
}
}
}
count=0;
}
if (word) {
mostUsedWord.innerHTML = `${word} (${initialCount} times)`
} else {
mostUsedWord.innerHTML = ""
}
}
document.getElementById("word-input").onkeypress = function() {
getCharacterCount();
getWordCount();
getSentenceCount();
getMostUsedWord();
}
getCharacterCount();
getWordCount();
getSentenceCount();
getMostUsedWord();