-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.js
88 lines (76 loc) · 1.92 KB
/
birthday.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
let people = document.getElementById("people");
paradox();
function paradox() {
if(people.value > -1){
if(people.value % 1 == 0){
pairs = (people.value)*(people.value - 1)/2;
chance_per_pair = (people.value-1)*(people.value)/730;
chance_pair = (2.718281828459045)**(-chance_per_pair);
chance = (100 - (chance_pair*100)).toFixed(4);
document.getElementById("show").innerHTML = "<br>Number of pairs: <b>" +pairs+ "</b><br>Average number of matching pairs: <b>" +(chance_per_pair).toFixed(4)+ "</b><br>Chance of 2 people in the room having same birthday: <b>"+chance+"%</b>";
}
else{
document.getElementById("show").innerHTML = "<br>Decimal inputs are not allowed";
}
}
else{
document.getElementById("show").innerHTML = "<br>Number of people cannot be negative";
}
}
function reset() {
people.value = '';
pairs = 0;
chance = 0.0000;
chance_per_pair = 0.0000;
paradox();
}
let exp1 = "100-(2.718281828459045**((x-x**2)/730))*100";
let exp2 = "(2.718281828459045**((x-x**2)/730))*100";
const xValues = [];
const yValues = [];
const y1Values = [];
for (let x = 0; x <= 110; x += 1) {
xValues.push(x);
yValues.push(eval(exp1));
}
for (let x = 0; x <= 110; x += 1) {
xValues.push(x);
y1Values.push(eval(exp2));
}
const data = [{
x: xValues,
y: y1Values,
mode:"lines",
line:{color:'blue'},
name:'Chance of no match'},
{x: xValues,
y: yValues,
mode:"lines",
line:{color:'red'},
name:'Chance of match'}
];
const layout = {
title: {
text:'Visualization data plot',
font: {
size: 24
},
},
xaxis: {
title: {
text: 'Number of people',
font: {
size: 20
},
},
},
yaxis: {
title: {
text: 'Chance of match (in %)',
font: {
size: 20
},
}
}
};
Plotly.newPlot("myPlot", data, layout, {displaylogo: false});