-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
120 lines (114 loc) · 5.16 KB
/
index.html
File metadata and controls
120 lines (114 loc) · 5.16 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!doctype html>
<html>
<head>
<title>
Text Analyzer
</title>
<script src="https://epicenterprograms.github.io/standards/behavior/general.js"></script>
<script>
var S = Standards.general;
var ALPHABET = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var PUNCTUATION = [".", "?", "!", ",", "'", '"', ";", ":", "-", "(", ")"];
var sortings = {"alphabetical":{}, "frequency":{}};
var usedCharacters = {};
var sortType = "frequency";
function display() {
document.getElementById("analysis").value = "Occurrences:\n\n";
let displayedList = {};
if (document.getElementById("combineLetters").checked) {
S.forEach(sortings[sortType], function(frequency, character) {
if (displayedList.hasOwnProperty(character.toLowerCase())) {
displayedList[character.toLowerCase()] += frequency;
} else {
displayedList[character.toLowerCase()] = frequency;
}
});
} else {
displayedList = sortings[sortType];
}
S.forEach(displayedList, function(frequency, character) {
document.getElementById("analysis").value += character + frequency + "\n";
});
}
window.addEventListener("load", function() {
document.getElementById("analyze").addEventListener("click", function() {
document.getElementById("before").style.display = "none";
var textArray = document.getElementById("text").value.split("").sort();
// sets the alphabetical version of the result
S.forEach(textArray, function(character) {
if (usedCharacters[character+" = "]) {
usedCharacters[character+" = "] += 1;
} else {
usedCharacters[character+" = "] = 1;
}
});
sortings.alphabetical = usedCharacters;
// sets the frequency-ordered version of the result
let sortingArray = [];
S.forEach(usedCharacters, function(frequency, character) {
sortingArray.push([character, frequency]);
});
sortingArray.sort(function(pair1, pair2) {
return pair2[1] - pair1[1];
});
S.forEach(sortingArray, function(pair) {
sortings.frequency[pair[0]] = pair[1];
});
// displays the result
display();
document.getElementById("after").style.display = "block";
});
document.getElementById("sort").addEventListener("click", function() {
sortType = sortType=="alphabetical" ? "frequency" : "alphabetical";
display();
});
document.getElementById("combineLetters").addEventListener("click", function() {
display();
});
document.getElementById("back").addEventListener("click", function() {
document.getElementById("text").value = "";
sortings = {"alphabetical":{}, "frequency":{}};
usedCharacters = {};
document.getElementById("analysis").value = "";
document.getElementById("after").style.display = "none";
document.getElementById("before").style.display = "block";
});
});
</script>
<link rel="stylesheet" href="https://epicenterprograms.github.io/standards/formatting/foundation.css">
<style>
#after {
display: none;
}
</style>
</head>
<body>
<h1 class="main-title">
Text Analyzer
</h1>
<main>
<div id="before">
<textarea cols="60" rows="10" placeholder="Put your text here." id="text"></textarea>
<br>
<br>
<button type="button" id="analyze">
Analyze
</button>
</div>
<div id="after">
<textarea cols="60" rows="10" id="analysis" readonly></textarea>
<br>
<br>
<input type="checkbox" id="combineLetters">
<label for="combineLetters">Combine capital and lowercase letters</label>
<br>
<button type="button" id="sort">
Sort differently
</button>
<button type="button" id="back">
Back
</button>
</div>
</main>
</body>
</html>