Skip to content

Instantly share code, notes, and snippets.

@jli-hashrocket
Created April 17, 2014 03:05
Show Gist options
  • Select an option

  • Save jli-hashrocket/10950115 to your computer and use it in GitHub Desktop.

Select an option

Save jli-hashrocket/10950115 to your computer and use it in GitHub Desktop.
$(function(){
var MemoryGame = function(){
this.init();
};
MemoryGame.prototype = jQuery.extend(MemoryGame.prototype, {
init: function(){
var deck = [1,5,3,5,7,8];
this.createDeck(deck);
this.handleClick();
},
createDeck: function(deck){
deck.forEach(function(num){
$('#gameContainer').append(function(){
var card = $('<div class="card"/>');
return card.html(num);
});
});
},
reset: function(clicked){
$('.card').css("color","#fff");
clicked = [];
},
handleClick: function(){
var clicked = [];
$('.card').click(function(event){
event.preventDefault();
$(this).css('color', '#000');
clicked.push($(this).html());
if(clicked.length == 2){
if(clicked[0] == clicked[1] ){
alert('This is a match!');
$('.card').css("color","#fff");
clicked = [];
}else{
alert('This is not a match :(');
$('.card').css("color","#fff");
clicked = [];
}
clicked = [];
}
});
}
});
memoryGame = new MemoryGame();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment