-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (65 loc) · 1.87 KB
/
index.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
// setup
let roundCount = 10000
let roundLength = 30
let results = [];
let resultLabels = [];
// dom elements
const ctx = document.getElementById('chart');
const roundCountLabel = document.querySelector("#roundCountLabel");
const roundCountInput = document.querySelector("#roundCountInput");
const roundLengthLabel = document.querySelector("#roundLengthLabel");
const roundLengthInput = document.querySelector("#roundLengthInput");
function flipCoin() {
return Math.floor(Math.random() * 2)
}
function generateData() {
results = Array(roundLength+1).fill(0)
resultLabels = Array.from({length: roundLength+1}, (v, i) => i)
for(let round = 0; round < roundCount; round++) {
let roundResult = 0
for(let i = 0; i < roundLength; i++) {
roundResult+=flipCoin()
}
results[roundResult]++
}
}
// Initial setup
roundCountInput.value = roundCount;
roundCountLabel.textContent = Number(roundCount).toLocaleString();
roundLengthInput.value = roundLength;
roundLengthLabel.textContent = roundLengthInput.value;
generateData();
const chart = new Chart(ctx, {
type: 'bar',
data: {
labels: resultLabels,
datasets: [{
label: 'Utfall totalt',
data: results,
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
// Regenerate when data is updated
roundCountInput.addEventListener("input", (event) => {
roundCount = parseInt(event.currentTarget.value);
roundCountLabel.textContent = Number(roundCount).toLocaleString();
generateData()
chart.data.datasets[0].data = results;
chart.update();
});
roundLengthInput.addEventListener("input", (event) => {
roundLength = parseInt(event.currentTarget.value);
roundLengthLabel.textContent = Number(roundLength).toLocaleString();
generateData()
chart.data.datasets[0].data = results;
chart.data.labels = resultLabels
chart.update();
});