-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
151 lines (131 loc) · 4.98 KB
/
index.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<html>
<head>
</head>
<body>
<textarea id="input" cols="70" rows="20"></textarea>
<textarea id="output" cols="70" rows="20"></textarea>
<br />
<select id="sort">
<option>First occurrence</option>
<option>Last occurrence</option>
<option>Most occurrences</option>
<option>Least occurrences</option>
<option>Ascending</option>
<option>Descending</option>
</select>
<button id="list">List</button>
<button id="table">Table</button>
<br />
<span id="status"></span>
<script>
document.getElementById('list').onclick = countStuff;
document.getElementById('table').onclick = countStuff;
function countStuff(e) {
var inputElem = document.getElementById('input');
var outputElem = document.getElementById('output');
var inputSplit = input.value.split('\n');
var counter = {};
inputSplit.forEach(function(i) {
counter[i] = (counter[i] || 0) + 1;
});
var sortElem = document.getElementById('sort');
var keys = Object.keys(counter);
if (e.target.id === "list") {
switch (sortElem.selectedIndex) {
case 0:
outputElem.value = keys.join('\n');
break;
case 1:
outputElem.value = keys.reverse().join('\n');
break;
case 2:
var items = keys.map(function(key) {
return [key, counter[key]];
});
items.sort(function(first, second) {
return second[1] - first[1];
});
var output = '';
for (var i = 0; i < keys.length; ++i) {
output += items[i][0] + '\n';
}
outputElem.value = output;
break;
case 3:
var items = keys.map(function(key) {
return [key, counter[key]];
});
items.sort(function(first, second) {
return second[1] - first[1];
});
var output = '';
for (var i = keys.length - 1; i >= 0; --i) {
output += items[i][0] + '\n';
}
outputElem.value = output;
break;
case 4:
outputElem.value = keys.sort().join('\n');
break;
case 5:
outputElem.value = keys.sort().reverse().join('\n');
break;
}
} else {
var output = '';
var values = Object.values(counter);
switch (sortElem.selectedIndex) {
case 0:
for (var i = 0; i < keys.length; ++i) {
output += keys[i] + '\t' + values[i] + '\n';
}
break;
case 1:
for (var i = keys.length - 1; i >= 0; --i) {
output += keys[i] + '\t' + values[i] + '\n';
}
break;
case 2:
var items = keys.map(function(key) {
return [key, counter[key]];
});
items.sort(function(first, second) {
return second[1] - first[1];
});
for (var i = 0; i < keys.length; ++i) {
output += items[i][0] + '\t' + items[i][1] + '\n';
}
break;
case 3:
var items = keys.map(function(key) {
return [key, counter[key]];
});
items.sort(function(first, second) {
return second[1] - first[1];
});
for (var i = keys.length - 1; i >= 0; --i) {
output += items[i][0] + '\t' + items[i][1] + '\n';
}
break;
case 4:
var keysSorted = keys.sort();
for (var i = 0; i < keys.length; ++i) {
output += keysSorted[i] + '\t' + counter[keysSorted[i]] + '\n';
}
break;
case 5:
var keysSorted = keys.sort();
for (var i = keys.length - 1; i >= 0; --i) {
output += keysSorted[i] + '\t' + counter[keysSorted[i]] + '\n';
}
break;
}
outputElem.value = output;
}
var diff = inputSplit.length - keys.length;
var text = (diff === 1) ? 'entry' : 'entries';
document.getElementById('status').innerText = diff + ' ' + text + ' removed.';
}
</script>
</body>
</html>