-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingo.js
66 lines (55 loc) · 1.45 KB
/
bingo.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
61
62
63
64
65
66
$(document).ready(function() {
var generateCards = function generateCards() {
var words, num, i, randomizedWords;
words = $("textarea").val().split("\n");
_.remove(words, function(word) { return !word; });
while (words.length < 24) {
words = words.concat(words);
}
num = $("input[name='count']").val();
$("#output").empty();
for (i = 0; i < num; i++) {
randomizedWords = randomizeCardWords(words);
$("#output").append(generateOneCard(randomizedWords));
}
$("#print").removeClass("disabled");
return false;
};
var randomizeCardWords = function randomizeCardWords(words) {
words = _.shuffle(words);
var matrix = [];
var i, j;
for (i = 0; i < 5; i++) {
if (!matrix[i]) {
matrix[i] = [];
}
for (j = 0; j < 5; j++) {
if (i == 2 && j == 2) {
matrix[i].push("*");
continue;
}
matrix[i].push(words.pop());
}
}
return matrix;
};
var generateOneCard = function generateOneCard(matrix) {
var $table = $("<table class='table table-bordered'><tr></tr></table>"),
$tr, $td;
var i, j;
for (i = 0; i < 5; i++) {
$table.find("tr").append($("<th>" + "BINGO".charAt(i) + "</th>"));
}
for (i = 0; i < 5; i++) {
$tr = $("<tr>")
for (j = 0; j < 5; j++) {
$td = $("<td>" + matrix[i][j] + "</td>");
$tr.append($td);
}
$table.append($tr);
}
return $table;
};
$("#generate").click(generateCards);
$("#print").click(function() {window.print(); });
});