forked from snej/oracles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (53 loc) · 1.54 KB
/
main.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
var NumItems = 4; // constant
var Oracle;
var Heading, Result, ResultList;
function setup() {
Heading = document.getElementById("heading");
ResultList = document.getElementById("result");
showIndex();
}
function showIndex() {
document.body.className = "index";
Heading.innerText = Heading.textContent = "Choose An Oracle:";
clearOracle();
for (var i = 0; i < OracleNames.length; ++i) {
var link = document.createElement("A");
link.href = "#";
link.index = i;
link.onclick = function() {showOracle(this.index);};
link.innerText = link.textContent = OracleNames[i];
item = document.createElement("LI");
item.appendChild(link);
ResultList.appendChild(item);
}
}
function showOracle(index) {
document.body.className = "oracle_"+index;
Heading.innerText = Heading.textContent = OracleNames[index];
Oracle = Oracles[index];
reloadOracle();
}
function reloadOracle() {
clearOracle();
var sayings = choose(Oracle, NumItems);
for (var i=0; i<sayings.length; ++i) {
item = document.createElement("LI");
item.innerText = item.textContent = "…" + sayings[i] + "…";
ResultList.appendChild(item);
}
}
function clearOracle() {
while (ResultList.firstChild)
ResultList.removeChild(ResultList.firstChild);
}
function randomItem(array) {
return array[Math.floor(Math.random() * array.length)];
}
function choose(oracle, n) {
result = n > 1 ? choose(oracle, n-1) : [];
var choice;
do {
choice = randomItem(oracle);
} while (result.indexOf(choice) >= 0);
return result.concat(choice);
}