-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.html
109 lines (89 loc) · 2.21 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
<html>
<body>
<h2>Is this your birthday?</h2>
<div id='dates'>
<div id='log-lower' class='log'></div>
<div><button id='lower' onclick='lower()'>Earlier</button></div>
<div id='date'>Date</div>
<div><button id='higher' onclick='higher()'>Later</button></div>
<div id='log-higher' class='log'></div>
</div>
<div id='count'>1 Guess</div>
</body>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
}
#dates {
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 20px;
}
#dates > div {
margin: 0 10px;
}
#date {
font-size: 25px;
}
.log {
font-family: 'Courier New';
color: gray;
}
</style>
<script>
let date = new Date()
let high = date
let low = new Date(0, 0, 1)
let size = 30
let n = 1
setDate()
function setDate(dir) {
document.getElementById('date').innerText = dateString()
const count = n + ' Guess' + (n > 1 ? 'es' : '')
document.getElementById('count').innerText = count
}
function dateString() {
return date.toString().substring(4, 15)
}
function higher() {
low = date
update('higher')
}
function lower() {
high = date
update('lower')
}
function update(dir) {
logDate(dir)
guess()
setDate(dir)
}
function guess() {
const delta = high.getTime() - low.getTime()
const error = delta * ((Math.random() / 2) - 0.25)
const guess = low.getTime() + (delta / 2) + error
date = new Date(guess)
n++
size--
}
function logDate(id) {
const elem = getDateElement(dateString())
if(id == 'lower') {
document.getElementById('log-higher').append(elem)
} else if(id == 'higher') {
document.getElementById('log-lower').prepend(elem)
}
}
function getDateElement(date) {
let text = document.createTextNode(date)
let elem = document.createElement('div')
elem.appendChild(text)
return elem
}
</script>
</html>