-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
170 lines (165 loc) · 5.52 KB
/
script.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function generate() {
// Get the input values
var candidate1 = document.getElementById("candidate1").value;
var candidate2 = document.getElementById("candidate2").value;
var candidate3 = document.getElementById("candidate3").value;
var candidate4 = document.getElementById("candidate4").value;
var candidate5 = document.getElementById("candidate5").value;
var candidate6 = document.getElementById("candidate6").value;
var candidate7 = document.getElementById("candidate7").value;
var candidate8 = document.getElementById("candidate8").value;
var predictions = "";
var total = 0;
var candidates = [{
name: "Petr Pavel",
value: candidate1
},
{
name: "Andrej Babiš",
value: candidate2
},
{
name: "Danuše Nerudová",
value: candidate3
},
{
name: "Pavel Fischer",
value: candidate4
},
{
name: "Jaroslav Bašta",
value: candidate5
},
{
name: "Marek Hilšer",
value: candidate6
},
{
name: "Karel Diviš",
value: candidate7
},
{
name: "Tomáš Zima",
value: candidate8
}
];
for (var i = 0; i < candidates.length; i++) {
if (candidates[i].value != "") {
total += parseFloat(candidates[i].value);
}
}
// Get the selected sorting, name format and whitespace options
var sortOption = document.getElementById("sortOption").value;
var nameOption = document.getElementById("nameOption").value;
var includePercentageSign = document.getElementById("percentageSignOption").checked;
var includeColon = document.getElementById("colonOption").checked;
var includeWhitespace = document.getElementById("whitespaceOption").checked;
// Sort the candidates based on the selected option
if (sortOption == "alphabetFirst") {
candidates.sort(function(a, b) {
if (a.name.split(" ")[0] < b.name.split(" ")[0]) {
return -1;
} else if (a.name.split(" ")[0] > b.name.split(" ")[0]) {
return 1;
} else {
return 0;
}
});
} else if (sortOption == "alphabetLast") {
candidates.sort(function(a, b) {
if (a.name.split(" ")[1] < b.name.split(" ")[1]) {
return -1;
} else if (a.name.split(" ")[1] > b.name.split(" ")[1]) {
return 1;
} else {
return 0;
}
});
} else {
candidates.sort(function(a, b) {
return b.value - a.value;
});
}
// Generate the sorted predictions string based on the selected options
predictions = "";
for (var i = 0; i < candidates.length; i++) {
if (candidates[i].value != "") {
if (nameOption == "last") {
var lastName = candidates[i].name.split(" ")[1];
if (includeColon) {
predictions += lastName + ": ";
} else {
predictions += lastName + " ";
}
if (includePercentageSign) {
if (includeWhitespace) {
predictions += candidates[i].value + " %\n";
} else {
predictions += candidates[i].value + "%\n";
}
} else {
predictions += candidates[i].value + "\n";
}
} else {
if (includeColon) {
predictions += candidates[i].name + ": ";
} else {
predictions += candidates[i].name + " ";
}
if (includePercentageSign) {
if (includeWhitespace) {
predictions += candidates[i].value + " %\n";
} else {
predictions += candidates[i].value + "%\n";
}
} else {
predictions += candidates[i].value + "\n";
}
}
}
}
// Copy the predictions to the clipboard
navigator.clipboard.writeText(predictions);
}
function updateTotal() {
var total = 0;
var candidates = [{
name: "Petr Pavel",
value: document.getElementById("candidate1").value
},
{
name: "Andrej Babiš",
value: document.getElementById("candidate2").value
},
{
name: "Danuše Nerudová",
value: document.getElementById("candidate3").value
},
{
name: "Pavel Fischer",
value: document.getElementById("candidate4").value
},
{
name: "Jaroslav Bašta",
value: document.getElementById("candidate5").value
},
{
name: "Marek Hilšer",
value: document.getElementById("candidate6").value
},
{
name: "Karel Diviš",
value: document.getElementById("candidate7").value
},
{
name: "Tomáš Zima",
value: document.getElementById("candidate8").value
}
];
for (var i = 0; i < candidates.length; i++) {
if (candidates[i].value != "") {
total += parseFloat(candidates[i].value);
}
}
document.getElementById("total").innerHTML = "Total Percentage: " + total + "%";
}