-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracketizer.js
More file actions
60 lines (56 loc) · 1.41 KB
/
bracketizer.js
File metadata and controls
60 lines (56 loc) · 1.41 KB
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
function createNode() {}
function bracketize(items) {
var halfLength = Math.ceil(items.length / 2);
var left = items.slice(0, halfLength);
var right = items.slice(halfLength, items.length);
var coinFlip = 0;
var leftNode = {};
if (left.length === 1) {
leftNode = {
base: true,
leftValue: left[0],
winner: left[0]
};
} else if (left.length === 2) {
coinFlip = Math.floor(Math.random() * Math.floor(2));
leftNode = {
base: true,
leftValue: left[0],
rightValue: left[1],
flipResult: coinFlip,
winner: left[coinFlip],
loser: left[1 - coinFlip]
};
} else {
leftNode = bracketize(left);
}
var rightNode = {};
if (right.length === 1) {
rightNode = {
base: true,
leftValue: right[0],
winner: right[0]
};
} else if (right.length === 2) {
coinFlip = Math.floor(Math.random() * Math.floor(2));
rightNode = {
base: true,
leftValue: right[0],
rightValue: right[1],
flipResult: coinFlip,
winner: right[coinFlip],
loser: right[1 - coinFlip]
};
} else {
rightNode = bracketize(right);
}
coinFlip = Math.floor(Math.random() * Math.floor(2));
return {
base: false,
left: leftNode,
right: rightNode,
flipResult: coinFlip,
winner: coinFlip === 0 ? leftNode.winner : rightNode.winner,
loser: coinFlip === 0 ? rightNode.winner : leftNode.winner
};
}