Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                clickCards(el){        
  // If the current card is already opened/ matched OR if 2 cards are already opened in this round, do nothing.      
  if(el.hasClass('open') || this.openCards.length == 2){
            return;
  }
 // Otherwise, do the normal checking procedures:
  
        this.clickCounter++;
        // clickCounter is set to 0 by default, add 1 to it since this click is valid. But later this counter will be set back to 0 after the checking procedures for this round is complete. See line 29.
  
        this.openCards.push(el);
        // Using 'openCards' array is a method to make sure only 2 cards are opened in each round. Add this card to 'openCards' array since this click is valid.
  
        
        el.addClass('open');
        // Aside from styling purpose, this 'open' class determines whether the card player later clicks on is already opened and/or already matched and opened. See line 3.
  

        if(this.clickCounter ==2){
            
            const cardNumber1 = this.openCards[0].data('number');
            const cardNumber2 = this.openCards[1].data('number');
            
            // 2 valid clicks equal to 1 valid turn. 
            this.stats.turns++;
           
            // Update display of no. of turns
            this._updateElement(this.el.turns, this.stats.turns);
          
            // Set clickCounter back to 0, so we can start counting clicks for a new round.
            this.clickCounter = 0;
            
        
            if(cardNumber1 === cardNumber2){
                notify.classList.add("active");
                notifyType.classList.add("matched");

                setTimeout(function(){
                notify.classList.remove("active");
                notifyType.classList.remove("matched");
                },2000);
            
                this.stats.matches++;
               
                this._updateElement(this.el.matches, this.stats.matches);
                
                this.openCards = [];
                
                if(this.stats.matches === this.winMatches){
                    this._endGame(true);
                }
            }else{
               
                this.stats.misses++;
                const that = this;
                setTimeout(function(){
                    that.openCards[0].removeClass('open');
                    that.openCards[1].removeClass('open');
                    
                    that.openCards = [];
                    
                    if(that.stats.turns >= 20) {
                        that._endGame(false);

                    }
                }, 1000)



// clickCards function will be fired when player clicks on card
playGame = true;
let game;

if (playGame){
    game = new Game();
    
    game.init();
   
    
    game.el.cards.click(function(){
      game.clickCards($(this));     
    });
    

}
              
            
!
999px

Console