-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
181 lines (171 loc) · 5.53 KB
/
stats.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
171
172
173
174
175
176
177
178
179
180
181
window.onload = function() {
datastr = sessionStorage.getItem('clickedNumbersHistory');
data = JSON.parse(datastr)
var gamesPlayed = document.getElementById('gamesPlayed');
gamesPlayed.innerHTML = `Games Played: ${data.length}`;
//Loop through each game and calculate the stats
var stats = {};
for (var i = 0; i < data.length; i++) {
var game = data[i];
for (var j = 0; j < game.length; j++) {
var number = game[j];
if (stats[number]) {
stats[number]++;
} else {
stats[number] = 1;
}
}
}
//From stats find top 10 numbers
var sortable = [];
for (var number in stats) {
sortable.push([number, stats[number]]);
}
sortable.sort(function(a, b) {
return b[1] - a[1];
});
var top10 = sortable.slice(0, 10);
var top10Numbers = document.getElementById('topTenNumbers');
top10Numbers.innerHTML = 'Top 10 Numbers: ' + top10.map(function(a) {
return a[0];
}).join(', ');
//Create a bar graph for the top 10 numbers where x is the number and y is the frequency
var ctx = document.getElementById('barGraph').getContext('2d');
var barGraph = new Chart(ctx, {
type: 'bar',
data: {
labels: top10.map(function(a) {
return a[0];
}),
datasets: [{
label: 'Frequency',
data: top10.map(function(a) {
return a[1];
}
),
backgroundColor: '#1E4D2B',
borderColor: '#1E4D2B',
borderWidth: 1
}]
},
options: {
scales: {
x: {
ticks: {
font: {
size: 50
}
}
},
y: {
ticks: {
font: {
size: 50
},
callback: function(value) {if (value % 1 === 0) {return value;}}
}
}
}
}
});
//Find all numbers that have not been called
letters = { 0: 'B', 1: 'I', 2: 'N', 3: 'G', 4: 'O'}
var notCalled = [];
for (var i = 1; i <= 75; i++) {
letter = letters[Math.floor((i-1) / 15)];
if (!stats[letter + i]) {
notCalled.push(letter + i);
}
}
var notCalledNumbers = document.getElementById('notCalledNumbers');
if (notCalled.length > 0)
{
notCalledNumbers.innerHTML = 'Numbers not called: ' + notCalled.join(', ');
}
else
{
notCalledNumbers.innerHTML = 'All Numbers were called!';
}
var sortable2 = [];
for (var number in stats) {
sortable2.push([number, stats[number]]);
}
for (var i = 0; i < notCalled.length; i++) {
console.log(notCalled[i]);
sortable2.push([notCalled[i], 0]);
}
function compareStrings(s1, s2) {
var match1 = s1.match(/(\D+)(\d+)/);
var match2 = s2.match(/(\D+)(\d+)/);
// Compare the letter parts
if (match1[1] < match2[1]) {
return -1;
} else if (match1[1] > match2[1]) {
return 1;
} else {
// If the letter parts are equal, compare the number parts
return parseInt(match1[2], 10) - parseInt(match2[2], 10);
}
}
sortable2.sort(function(a, b) {
return compareStrings(a[0], b[0])
});
//Create a bar graph for the all elements allGraph where x is the number and y is the frequency
var ctx2 = document.getElementById('allGraph').getContext('2d');
var allGraph = new Chart(ctx2, {
type: 'bar',
data: {
labels: sortable2.map(function(a) {
console.log(a[0])
return a[0];
}),
datasets: [{
label: 'Frequency',
data: sortable2.map(function(a) {
return a[1];
}
),
backgroundColor: '#1E4D2B',
borderColor: '#1E4D2B',
borderWidth: 1
}]
},
options: {
scales: {
x: {
ticks: {
font: {
size: 25
}
}
},
y: {
ticks: {
font: {
size: 25
},
callback: function(value) {if (value % 1 === 0) {return value;}}
}
}
}
}
});
// Create save button
var saveButton = document.createElement('button');
saveButton.innerHTML = 'Save Games';
saveButton.onclick = function() {
// Create a Blob with the data
var blob = new Blob([datastr], {type: 'text/plain'});
// Create a link for the Blob
var url = URL.createObjectURL(blob);
// Create a download link
var downloadLink = document.createElement('a');
downloadLink.download = 'clickedNumbersHistory.txt';
downloadLink.href = url;
// Trigger the download
downloadLink.click();
}
//center button increase size to 50px and add padding to to the top of 10px
saveButton.style = 'display: block; margin: auto; font-size: 40px; margin-top: 30px;';
document.body.appendChild(saveButton);
};