-
Notifications
You must be signed in to change notification settings - Fork 0
/
paranoid-debate.html
187 lines (169 loc) · 4.9 KB
/
paranoid-debate.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
<!doctype html>
<head>
<meta charset="utf-8">
<title>Параноидальные дебаты</title>
<style>
main {
margin: 0 auto;
max-width: 800px;
}
h1 > a {
color: #333;
}
input {
height: 30px;
display: block;
font-size: 24px;
width: 500px;
margin-bottom: 10px;
}
label {
font-size: 18px;
color: #333;
text-align: center;
}
button {
background-color: #f8f8f8;
border: 1px solid #333;
border-radius: 3px;
cursor: pointer;
display: block;
font-size: 24px;
padding: 4px 8px;
margin-bottom: 30px;
}
button:hover {
background-color: white;
}
.end-debate, .result {
display: none;
}
.labeled {
margin-bottom: 10px;
}
.labeled > div {
font-size: 24px;
}
.result.right .outcome, .result.right .new-average {
color: green;
}
.result.wrong .outcome, .result.wrong .new-average {
color: red;
}
.new-average {
font-weight: bold;
}
</style>
</head>
<body>
<main>
<h1>
<a href="https://lesswrong-ru.hackpad.com/--UdZGQYxpdbS">
Параноидальные дебаты
</a>
</h1>
<div class="initial">
<label>Изначальные оценки (через пробел):</label>
<input id="initial-answers" type="text" pattern="^[\d\s]+$"/>
<button>Начать обсуждение</button>
</div>
<div class="end-debate">
<label>Ответ капитана:</label>
<input id="captain-answer" type="number" />
<label>Настоящий ответ:</label>
<input id="real-answer" type="number" />
<button>Подвести итоги</button>
</div>
<div class="result">
<div class="labeled">
<label>Наиболее точная оценка (+10 тому, кто её назвал):</label>
<div class="closest-answer"></div>
</div>
<div class="labeled">
<label>Изначальная медиана:</label>
<div class="old-average"></div>
</div>
<div class="labeled">
<label>Ответ капитана:</label>
<div class="new-average"></div>
</div>
<div class="labeled">
<label>Итого:</label>
<div class="outcome"></div>
</div>
</div>
</main>
<script>
function $(selector) {
return document.querySelector(selector);
};
function $$(el, selector) {
return el.querySelector(selector);
}
function reveal(el) {
el.style.display = "block";
};
function hide(el) {
el.style.display = "none";
};
function delta(a, b) {
return Math.abs(a - b);
}
function median(list) {
var sorted = list.concat().sort(function (a, b) { return a - b });
if (sorted.length % 2 === 1) {
return sorted[Math.floor(sorted.length / 2)];
} else {
return (
sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]
) / 2;
}
}
function average(list) {
return list.reduce(function (a, b) { return a + b }) / list.length;
}
var initialEl = $(".initial");
$$(initialEl, "button").onclick = function () {
reveal($(".end-debate"));
};
var endDebateEl = $(".end-debate");
$$(endDebateEl, "button").onclick = function () {
var initialAnswers = $("#initial-answers").value.trim().split(/\s+/).map(
function (a) {
return parseFloat(a);
}
);
var captainAnswer = parseFloat($("#captain-answer").value);
var realAnswer = parseFloat($("#real-answer").value);
var minDelta = undefined;
var minId = undefined;
for (var i = 0; i < initialAnswers.length; i++) {
if (minDelta === undefined || delta(initialAnswers[i], realAnswer) < minDelta) {
minId = i;
minDelta = delta(initialAnswers[i], realAnswer);
}
}
var closestAnswer = initialAnswers[minId];
var resultEl = $(".result");
$$(resultEl, ".closest-answer").innerHTML = closestAnswer;
var oldAverage = median(initialAnswers);
$$(resultEl, ".old-average").innerHTML = oldAverage;
var captainAnswerEl = $$(resultEl, ".new-average");
captainAnswerEl.innerHTML = captainAnswer;
var originalDelta = delta(oldAverage, realAnswer);
var finalDelta = delta(captainAnswer, realAnswer);
var teamWon = finalDelta < originalDelta;
resultEl.classList.remove(teamWon ? "wrong" : "right");
resultEl.classList.add(teamWon ? "right" : "wrong");
if (teamWon) {
var closer = Math.round(10 * (originalDelta - finalDelta) / originalDelta);
$(".outcome").innerHTML = "+" + closer + " всем, кроме предателя.";
}
else {
var farther = Math.min(Math.round(10 * (finalDelta - originalDelta) / originalDelta), 10);
$(".outcome").innerHTML = "+" + farther + " предателю.";
}
reveal(resultEl);
};
</script>
</body>