-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionsjs_chanceAmount.html
290 lines (290 loc) · 16.2 KB
/
functionsjs_chanceAmount.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="[MAZ01001.github.io] Calculate the chance of success for consecutive repeats of events with equal chances">
<meta name="author" content="MAZ01001">
<link rel="apple-touch-icon" sizes="180x180" href="../img/apple-touch-icon.png">
<link rel="icon" type="image/x-icon" href="../img/MAZ_logo.svg">
<link rel="icon" type="image/png" sizes="32x32" href="../img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../img/favicon-16x16.png">
<link rel="manifest" href="../img/site.webmanifest">
<link rel="mask-icon" href="../img/safari-pinned-tab.svg" color="#ff9900">
<link rel="shortcut icon" href="../img/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="../img/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<title>Chance Calculator</title>
<style>
*{
scrollbar-width: thin;
scrollbar-color: #0a07 #0000;
}
*:hover{scrollbar-color: #0a0 #0000;}
::selection{
background-color: #f90;
color: #000;
}
body{
background-color: #000;
color: #0f0;
font-family: "consolas", monospace;
font-size: medium;
color-scheme: dark;
cursor: default;
}
fieldset>legend{cursor: help;}
input[type=radio]{
cursor: pointer;
margin-inline-end: .75rem;
accent-color: #0f0;
}
input{
font: inherit;
color: inherit;
}
input[type=number]{
background-color: #000;
border: 2px inset #000;
width: 3.3rem;
height: 1rem;
margin-inline: 2px;
}
input[type=number]:read-only{
cursor: default;
background-color: #222;
}
input:invalid{border-color: #f00;}
input:invalid:focus-visible{
outline-style: solid;
outline-color: #f00;
}
input[type=button]{
cursor: pointer;
font-size: small;
border-color: #000;
background-color: #444;
}
input[type=button]:disabled{
cursor: not-allowed;
color: #0a0;
border: 2px solid #7777;
background-color: #333;
}
input[type=button]:enabled:active{background-color: #555;}
input[type=button]:disabled:active{border-color: #722;}
div#box{
position: fixed;
top: 50%;
left: 50%;
translate: -50% -50%;
max-width: 100vw;
max-height: 100vh;
width: max-content;
height: max-content;
overflow: auto;
padding: 1rem;
box-sizing: border-box;
background-color: #222;
border: 2px outset #444;
}
div#box>fieldset{min-width: max-content;}
fieldset#chanceCalc input[type=number]{width: 5.5rem;}
</style>
<script>
"use strict";
/**
* ## Calculate the number of consecutive tries needed until an event with a given % change has a 90% (or custom) chance of success overall
* set two variables to calculate the missing (or `null`) third: {@linkcode tries} consecutive tries with {@linkcode chance}% each to have a {@linkcode goal}% chance of success overall \
* give percentages in decimal values (`[0,1]`); also tries can have decimals (`[0,∞[`)
* @param {number|null} [chance] - percentage of the chance for given event succeeding (once) - default none (expected to be given)
* @param {number|null} [tries] - number of consecutive tries (can be decimal) - default `null` (expected to be the output)
* @param {number|null} [goal] - percentage of the final/total chance of success for given event (after {@linkcode tries} number of consecutive tries) - default 90% (`0.90`)
* @returns {number} parameter that was not given (or `null`): {@linkcode chance}, {@linkcode tries}, or {@linkcode goal}
* @example // in explanation: (input) {default} [output]
* chanceAmount(0.40); //=> 4.507575551943848 | [4.51] consecutive tries with (40%) each to have a {90%} chance of success overall
* chanceAmount(0.40, null, 0.50); //=> 1.3569154488567239 | [1.36] consecutive tries with (40%) each to have a (50%) chance of success overall
* chanceAmount(0.40, 2); //=> 0.64 | (2) consecutive tries with (40%) each to have a [64%] chance of success overall
* chanceAmount(null, 2, 0.50); //=> 0.2928932188134524 | (2) consecutive tries with [29.29%] each to have a (50%) chance of success overall
* @throws {TypeError} if {@linkcode chance}, {@linkcode tries}, or {@linkcode goal} are not numbers (not type number or `NaN`)
* @throws {RangeError} if {@linkcode chance} or {@linkcode goal} are negative or above 100% (`1.00`)
* @throws {RangeError} if {@linkcode tries} is negative or `Infinity`
* @throws {SyntaxError} if an invalid set of parameters are given (only two parameters are given (missing is `null` or `undefined`))
* @typedef {(chance:number,tries?:null,goal?:number)=>number} CalcTries calculate how many consecutive tries, with {@linkcode chance}% each, are needed to have a {@linkcode goal}% (default 90%) chance of success overall (that it happens once)
* @typedef {(chance:number,tries:number,goal?:null)=>number} CalcGoal calculate how much chance of success (that it happens once) there is overall, for {@linkcode tries} consecutive tries with {@linkcode chance}% each
* @typedef {(chance:undefined|null,tries:number,goal:number)=>number} CalcChance calculate what % chance each try has, when {@linkcode tries} consecutive tries have a {@linkcode goal}% chance of success overall (that it happens once)
* @type {CalcChance&CalcTries&CalcGoal}
*/
const chanceAmount=(chance,tries,goal)=>{
if(chance!=null){
if(typeof chance!=="number"||Number.isNaN(chance))throw new TypeError("[chanceAmount] chance is not a number");
if(chance<0||chance>1)throw new RangeError("[chanceAmount] chance can't be negative or above 100%");
}
if(tries!=null){
if(typeof tries!=="number"||Number.isNaN(tries))throw new TypeError("[chanceAmount] tries is not a number");
if(tries<0||tries===Infinity)throw new RangeError("[chanceAmount] tries can't be negative or infinite");
}
if(goal!=null){
if(typeof goal!=="number"||Number.isNaN(goal))throw new TypeError("[chanceAmount] goal is not a number");
if(goal<0||goal>1)throw new RangeError("[chanceAmount] goal can't be negative or above 100%");
}
//~ tries = log[1 - chance](1 - goal) = log(1 - goal) / log(1 - chance)
if(chance!=null&&tries==null){
//? avoid NaN for chance:0% and goal:0% → tries:0
if(goal===0)return 0;
//? avoid NaN for chance:100% and goal:100% → tries:1
if(chance===1)return 1;
return Math.log1p(-(goal??.9))/Math.log1p(-chance);
}
//~ goal = 1 - (1 - chance)^tries
if(chance!=null&&tries!=null&&goal==null)return 1-(1-chance)**tries;
//~ chance = 1 - root[tries](1 - goal) = 1 - (1 - goal)^(1 / tries)
if(chance==null&&tries!=null&&goal!=null){
//? avoid NaN for tries:0 and goal:0% → chance:0%
if(goal===0)return 0;
return 1-(1-goal)**(1/tries);
}
throw new SyntaxError("[chanceAmount] invalid set of parameters given");
};
</script>
</head>
<body>
<noscript>JavaScript must be enabled!</noscript>
<div id="box">
<fieldset id="chanceCalc"><legend title="Select the output with the bullet points on the left and then edit the other two values">Chance Calculator</legend>
<p><input type="radio" name="out" id="out_chance"><label for="chance"><input type="number" id="chance" min="0" max="100" step="any" autofocus required><span>% success chance for one try</span></label></p>
<p><input type="radio" name="out" id="out_tries" checked><label for="tries"><input type="number" id="tries" min="0" step="any" readonly><span>number of consecutive tries</span></label></p>
<p><input type="radio" name="out" id="out_goal"><label for="goal"><input type="number" id="goal" min="0" max="100" step="any" value="90" required><span>% total chance of success</span></label></p>
</fieldset>
<br>
<fieldset id="rangeCalc"><legend title="convert any number within the given range to a percentage; for use in the chance calculator">Range Calculator</legend>
<p>
<input type="number" id="x" step="any" required placeholder="x" title="Range value">
<label for="min">from <input type="number" id="min" step="any" value="0" required placeholder="min" title="Range start value"></label>
<label for="max">to <input type="number" id="max" step="any" required placeholder="max" title="Range end value"></label>
<label for="p">⇒ <input type="number" id="p" step="any" placeholder="--" readonly title="Percentage of value within given range">%</label>
</p>
<input type="button" id="cpy_chance" title="click to use the calculated percentage as the single chance in the chance calculator" value="copy to single chance">
<input type="button" id="cpy_goal" title="click to use the calculated percentage as the total chance in the chance calculator" value="copy to total chance">
</fieldset>
</div>
<script>
"use strict";
const html=Object.freeze({
/**@type {HTMLInputElement}*/out_chance:document.getElementById("out_chance"),
/**@type {HTMLInputElement}*/out_tries:document.getElementById("out_tries"),
/**@type {HTMLInputElement}*/out_goal:document.getElementById("out_goal"),
/**@type {HTMLInputElement}*/chance:document.getElementById("chance"),
/**@type {HTMLInputElement}*/tries:document.getElementById("tries"),
/**@type {HTMLInputElement}*/goal:document.getElementById("goal"),
/**@type {HTMLInputElement}*/x:document.getElementById("x"),
/**@type {HTMLInputElement}*/min:document.getElementById("min"),
/**@type {HTMLInputElement}*/max:document.getElementById("max"),
/**@type {HTMLInputElement}*/p:document.getElementById("p"),
/**@type {HTMLInputElement}*/cpy_chance:document.getElementById("cpy_chance"),
/**@type {HTMLInputElement}*/cpy_goal:document.getElementById("cpy_goal"),
});
html.out_chance.addEventListener("change",()=>{
html.chance.required=!(html.cpy_chance.disabled=html.chance.readOnly=true);
html.tries.required=!(html.tries.readOnly=false);
html.goal.required=!(html.cpy_goal.disabled=html.goal.readOnly=false);
chance();
},{passive:true});
html.out_tries.addEventListener("change",()=>{
html.chance.required=!(html.cpy_chance.disabled=html.chance.readOnly=false);
html.tries.required=!(html.tries.readOnly=true);
html.goal.required=!(html.cpy_goal.disabled=html.goal.readOnly=false);
chance();
},{passive:true});
html.out_goal.addEventListener("change",()=>{
html.chance.required=!(html.cpy_chance.disabled=html.chance.readOnly=false);
html.tries.required=!(html.tries.readOnly=false);
html.goal.required=!(html.cpy_goal.disabled=html.goal.readOnly=true);
chance();
},{passive:true});
/**calculate chance according to html elements and set output html*/
const chance=()=>{
html.tries.placeholder="";
if(html.tries.readOnly){
const chance=Number(html.chance.value||NaN)/100,
goal=Number(html.goal.value||NaN)/100;
if(
Number.isNaN(chance)||chance>1||chance<0
||Number.isNaN(goal)||goal>1||goal<0
)html.tries.value="";
else{
const out=chanceAmount(chance,null,goal);
if(out===Infinity){
html.tries.value="";
html.tries.placeholder="Infinity";
}else html.tries.value=String(out);
}
}else if(html.goal.readOnly){
const chance=Number(html.chance.value||NaN)/100,
tries=Number(html.tries.value||NaN);
if(
Number.isNaN(chance)||chance>1||chance<0
||Number.isNaN(tries)||tries<0
)html.goal.value="";
else html.goal.value=String(chanceAmount(chance,tries)*100);
}else if(html.chance.readOnly){
const tries=Number(html.tries.value||NaN),
goal=Number(html.goal.value||NaN)/100;
if(
Number.isNaN(tries)||tries<0
||Number.isNaN(goal)||goal>1||goal<0
)html.chance.value="";
else html.chance.value=String(chanceAmount(null,tries,goal)*100);
}
};
html.chance.addEventListener("input",chance,{passive:true});
html.tries.addEventListener("input",chance,{passive:true});
html.goal.addEventListener("input",chance,{passive:true});
/**@type {(n:number,x:number,y:number)=>number}*/
const map=(n,x,y)=>y<x?(n-y)/(x-y):(n-x)/(y-x);
/**calculate range according to html elements and set output html*/
const range=()=>{
const x=Number(html.x.value||NaN),
min=Number(html.min.value||NaN),
max=Number(html.max.value||NaN);
if(Number.isNaN(x)||Number.isNaN(min)||Number.isNaN(max))html.p.value="";
else html.p.value=String(map(x,min,max)*100);
};
html.x.addEventListener("input",range,{passive:true});
html.min.addEventListener("input",range,{passive:true});
html.max.addEventListener("input",range,{passive:true});
html.cpy_chance.addEventListener("click",()=>{
html.chance.value=html.p.value;
html.chance.dispatchEvent(new InputEvent("input"));
},{passive:true});
html.cpy_goal.addEventListener("click",()=>{
html.goal.value=html.p.value;
html.goal.dispatchEvent(new InputEvent("input"));
},{passive:true});
/**@type {(ev:KeyboardEvent,el:HTMLInputElement)=>void} ! can call {@linkcode Event.preventDefault}*/
const keyInc=(ev,el)=>{
let dir=ev.key==="ArrowUp"?1:ev.key==="ArrowDown"?-1:0;
if(dir===0)return;
el.value=String(Number(el.value));
if(/e/i.test(el.value))return;
ev.preventDefault();
if(ev.shiftKey)dir*=10;
if(ev.ctrlKey)dir*=100;
if(ev.altKey)dir=.1/dir;
const[,n,r]=el.value.match(/^(\d*(?:\.\d{0,5})?)(\d*?)$/)??[,"",""];
const num=Number((Number(n)+dir).toFixed(5)+r);
if(num<Number(el.min||NaN))el.value=el.min;
else if(num>Number(el.max||NaN))el.value=el.max;
else el.value=String(num);
el.dispatchEvent(new InputEvent("input"));
}
html.chance.addEventListener("keydown",ev=>keyInc(ev,html.chance),{passive:false});
html.tries.addEventListener("keydown",ev=>keyInc(ev,html.tries),{passive:false});
html.goal.addEventListener("keydown",ev=>keyInc(ev,html.goal),{passive:false});
html.x.addEventListener("keydown",ev=>keyInc(ev,html.x),{passive:false});
html.min.addEventListener("keydown",ev=>keyInc(ev,html.min),{passive:false});
html.max.addEventListener("keydown",ev=>keyInc(ev,html.max),{passive:false});
</script>
</body>
</html>