-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (91 loc) · 2.79 KB
/
index.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
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
var suits = ["hearts", "diams", "clubs", "spades"];
var ranks = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"];
var deck = [];
$(init);
function init() {
makeDeck();
$("#drawbtn").click(function () {
var myCard = drawCard();
if (myCard) {
makeCard(myCard.suit, myCard.rank);
} else {
alert("no more cards in the deck");
}
});
}
function makeDeck() {
deck = [];
//for each type of suit
for (var i = 0; i < suits.length; i++) {
//and for each rank
for (var j = 0; j < ranks.length; j++) {
//make a card
var card = {};
card.suit = suits[i];
card.rank = ranks[j];
deck.push(card);
}
}
console.log(deck.length);
console.log("MADE A NEW DECK OF ", deck.length, " CARDS");
console.log(deck);
}
function drawCard() {
var card;
if (deck.length > 0) {
var randIndex = Math.floor(Math.random() * deck.length);
card = deck.splice(randIndex, 1)[0];
}
return card;
}
function makeCard(suit, rank) {
var card = $(".card.template").clone();
card.removeClass("template");
card.find(".rank").html(rank);
card.find(".suit").html("&" + suit + ";");
if (suit === "clubs" || suit === "spades") {
//alert("hi");
card.attr('id', rank);
}
if (suit === "hearts" || suit === "diams") {
card.addClass("red");
card.attr('id', rank);
}
$("#viewCards").append(card);
}
$("#shufflebtn").click(function () {
var section = document.querySelector('section');
for (var i = section.children.length; i >= 0; i--) {
section.appendChild(section.children[Math.random() * i | 0]);
}
})
$("#sortbtn").click(function () {
// var list = document.querySelector('section');
// var items = list.childNodes;
// var itemsArr = [];
// for (var i in items) {
// if (items[i].nodeType == 1) { // get rid of the whitespace text nodes
// itemsArr.push(items[i]);
// }
// }
// itemsArr.sort(function (a, b) {
// return a.innerHTML == b.innerHTML
// ? 0
// : (a.innerHTML > b.innerHTML ? 1 : -1);
// });
// for (i = 0; i < itemsArr.length; ++i) {
// list.appendChild(itemsArr[i]);
// }
var $sorted_items,
getSorted = function (selector, attrName) {
return $(
$(selector).toArray().sort(function (a, b) {
var aVal = parseInt(a.getAttribute(attrName)),
bVal = parseInt(b.getAttribute(attrName));
return aVal - bVal;
})
);
};
$sorted_items = getSorted('#viewCards .card', 'id').clone();
$('#viewCards').html($sorted_items);
})