You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cards: [0,0,1,1,2,2,3,3,4,4,5,5,6,6,],//array of cards
init: function(){
app.shuffle();
},
shuffle: function(){//card shuffling function
varrandom=0;
vartemp=0;
for(i=0;i<app.cards.length;i++){//iterates through the number of cards 14 times and each time it will present a random number
random=Math.round(Math.random()*i);
//displays 14 random numbers in the console log
temp=app.cards[i];
app.cards[i]=app.cards[random];
app.cards[random]=temp;//stores the current index number into app.cards, then set it into the temp variable then set app.cards into random, and random back to temp
};
app.setCards();
//displays the order the cards were shuffled in the console
},
setCards: function(){//this function sets a random value to the cards
$('.cards').each(function(index){
$(this).attr('data-card-value',app.cards[index]);//the .attr gets the attribute value on only the first element
app.cards[index];
});
app.clickEvents();
},
clickEvents: function(){
$('.cards').on('click',function(){
$(this).html('<p>'+$(this).data('cardValue')+'</p>').addClass('selected');//adds the "selected" class to clicked cards
app.checkGame();
});
},
checkGame: function(){
if($('.selected').length===2){
if($('.selected').first().data('cardValue')==$('.selected').last().data('cardValue')){//checks to see if the first and last cards clicked, match. If they do remove the cards
$('.selected').each(function(){
points++;
varpointsOutside=$('#points').html(points+" points");//add 2 points if the cards match
$(".myAudioElement")[0].play();//play audio
$(this).animate({
opacity: 0
}).removeClass('notmatched');//if the they match then hide them with opacity and remove 'notmatched' from the class
});
$('.selected').each(function(){
$(this).removeClass('selected');
});
app.checkWin();
}else{
setTimeout(function(){
$('.selected').each(function(){
$(this).html('').removeClass('selected');
});
},1000);//removes the selected class, and turns them to their orignal class if 2 cards dont match
}
}
},
checkWin: function(){//if there are 0 classes with the "notmatched" class then the game is over!
if($('.notmatched').length===0){
$('.game').html("<h1> All Cards Matched! </h1>"+"<h2> CLICKS: "+number+"<h2> POINTS: "+points+"</h2>");