-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
118 lines (111 loc) · 3.53 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Random Set Completer</title>
<link rel="stylesheet" href="https://bootswatch.com/4/journal/bootstrap.min.css" />
<style>
#footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-6 col-sm-8 col-10 mx-auto mt-3">
<div class="card bg-primary p-5">
<form id="params">
<div class="form-group">
<label class="font-weight-bold" for="min">Min</label>
<input type="number" class="form-control" name="min" id="min" value="1" />
</div>
<div class="form-group">
<label class="font-weight-bold" for="max">Max</label>
<input type="number" class="form-control" name="max" id="max" value="10" />
</div>
<div class="form-group">
<label class="font-weight-bold" for="cycles">Cycles</label>
<input type="number" class="form-control" name="cycles" id="cycles" value="10" />
</div>
<input type="submit" class="btn btn-light btn-block" value="Go!" />
</form>
</div>
</div>
</div>
<div class="row">
<div id="result" class="col-lg-4 col-md-6 col-sm-8 col-10 mx-auto mt-3">
</div>
</div>
<div class="row">
<div class="col-sm-8 col-10 mx-auto mt-3">
<table id="results-table" class="table table-striped">
<thead class="thead-dark text-center">
<tr>
<th>Cycle #</th>
<th>Roll count</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<div id="footer" class="bg-dark text-center text-light p-3">
This is a free tool designed by <a href="https://www.github.com/etoler1841">Eric Toler</a>.
View the source on <a href="https://www.github.com/etoler1841/random-set-completion">GitHub</a>.
</div>
<script>
document.querySelector("#params").addEventListener("submit", e => {
e.preventDefault()
let min = document.querySelector("#min").value
let max = document.querySelector("#max").value
let cycles = document.querySelector("#cycles").value
let results = []
document.querySelector("#results-table tbody").innerHTML = ""
for(let i = 1; i <= cycles; i++) {
let res = rollToCompletion(min, max)
document.querySelector("#results-table tbody").innerHTML += (
`<tr>
<td>${ i }</td>
<td>${ res }</td>
</tr>`
)
results.push(res)
}
let sum = 0
for(let i = 0; i < results.length; i++) {
sum += results[i]
}
let avg = sum / results.length
document.querySelector("#result").innerHTML = (
`<div class="card bg-light p-3">
Average rolls required: ${ avg }
</div>`
)
})
const rollToCompletion = (min, max) => {
min = parseInt(min)
max = parseInt(max)
let rolls = []
for(let i = min; i <= max; i++) {
rolls.push(0)
}
let i = 0
while(true) {
let rand = Math.floor(Math.random() * (max - min + 1))
rolls[rand]++
i++
if(rolls.indexOf(0) === -1) break
}
return i
}
</script>
</body>
</html>