-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
105 lines (84 loc) · 3.15 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Monty Hall Problem</title>
</head>
<body>
<h1>The Monty Hall Problem</h1>
<script>
// Define a variable to determine how many times to repeat the script
let repeat = 10000;
// Define a variable to track how many times stay would be the right choice
let stay = 0;
// Loop using the repeat variable
for(let i = 0; i < repeat; i++)
{
// Step 1
// Place the prize behind a random door
let winningDoor = Math.ceil(Math.random() * 3);
document.write("<p>The winning door is number " + winningDoor + ".</p>");
// Step 2
// Winner needs to choose a door
let choice = Math.ceil(Math.random() * 3);
document.write("<p>The chosen door is number " + choice + ".</p>");
// Step 3
// Remove a non-winning non-chosen door
// Method 1
// Created a list of doors that could be removed
// Randomly removed one of those door
/*
let doors = [];
for(let i = 1; i <= 3; i ++)
// If the removing door is the winning door or the chosen door, then
// choose another random door
while(remove == choice || remove == winningDoor)
{
if(i != winningDoor && i != choice)
{
doors.push(i);
}
}
let removeKey = Math.floor(Math.random() * doors.length);
let removeDoor = doors[removeKey];
document.write("<p>The random key is " + removeKey + " for array " + doors.join(",") + ".</p>");
document.write("<p>The removed door is number " + removeDoor + ".</p>");
*/
// Method 2
// Choose a random door to remove
// Check the random door (non-winning non-chosen)
// Repeat
/*
let removeDoor = Math.ceil(Math.random() * 3);
while(removeDoor == choice || removeDoor == winningDoor)
{
removeDoor = Math.ceil(Math.random() * 3);
}
*/
let removeDoor;
do
{
removeDoor = Math.ceil(Math.random() * 3);
}
while(removeDoor == choice || removeDoor == winningDoor);
document.write("<p>The removed door is number " + removeDoor + ".</p>");
// Step 4
// Display the results
if(winningDoor == choice)
{
document.write("<h2>STAY!</h2>");
stay ++;
}
else
{
document.write("<h2>SWITCH!</h2>");
}
document.write("<hr>");
}
let percent = (stay / repeat * 100).toFixed(2);
document.write("<p>Percent STAY is the right choice is " + percent + "%.</p>");
</script>
</body>
</html>