-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (60 loc) · 1.67 KB
/
script.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
$(function(){
open_random_card()
randomize()
/*setInterval(function(){
randomize()
}, 5000)*/
$('.card').click(function(){
var $this = $(this)
if($this.hasClass('correct')){
alert('Already correct')
return
}
$this.addClass('clicked')
if($('.clicked').length === 2){
check_cards()
}
})
})
function check_cards() {
var card_one = $('.clicked').eq(0),
card_two = $('.clicked').eq(1);
setTimeout(function(){
if(card_one.attr('class') === card_two.attr('class')){
alert('Correct!')
card_one.addClass('correct')
card_two.addClass('correct')
reset_clicked_card()
check_if_won()
} else {
alert('Wrong ...')
reset_clicked_card()
}
}, 1000)
}
function reset_clicked_card() {
$('.card').removeClass('clicked')
card_one = undefined
card_two = undefined
}
function check_if_won() {
if($('.card.correct').length === $('.card').length){
alert('You won :)')
}
}
function randomize(){
var memory = $('#memory');
for (var i = memory.children().length; i >= 0; i--) {
memory.append(memory.children()[Math.random() * i | 0]);
}
}
function open_random_card() {
var rand = Math.round(Math.random() * $('.card').length)
setTimeout(function(){
$('.card').eq(rand).addClass('clicked')
setTimeout(function(){
$('.card').eq(rand).removeClass('clicked')
open_random_card()
}, 3000)
}, 3000)
}