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

              
                <h1>Social Concentration Game</h1>
<p>This is a simple concentration game made with pure javascript without any library. In the boxes you will see social media icons. Can you find same social media icons repeatedly ? Thanks Font Awesome for icons!</p>
<div id="container">
  
</div>
              
            
!

CSS

              
                h1{
  text-align:center;
  font-size:2em;
  font-family:Arial;
  color:#333;
  margin-bottom:10px;
}

p{
  text-align:center;
  width:500px;
  margin:auto;
  margin-bottom:20px;
  font-family:Arial;
  font-size:12px;
  line-height:18px;
  color:#666;
}

#container{
  background:#eee;
  border:1px solid #ddd;
  width:460px;
  height:370px;
  margin:auto;
}

.tile_box{
  display:block;
  float:left;
  width:80px;
  height: 80px;
  background:#ddd;
  margin-left:10px;
  margin-top:10px;
  text-decoration:none;
  color:#333;
  font-family:Arial;
  text-align:center;
  line-height:80px;
  font-size:32px;
  cursor:pointer;
  opacity:.5;
}

.tile_box:hover{
  opacity:1;
}

.removed{
  background:#eee !important;
  cursor:default !important;
}
              
            
!

JS

              
                //tile object
function tile_box(){
  this.tile_type;
  this.addToScene = function(id){
    document.getElementById('container').innerHTML += '<span href="#" id="'+id+'" data-type="'+this.tile_type+'" class="tile_box"><i class="fa fa-question"></i></span>';
  }
}

//variables
var number_of_tiles = 20;
var tiles_per_row = 5;
var openings = 0;
var tries = 0;
var tiles = new Array();
var tile = new tile_box();
var picked_tiles = new Array();
var can_pick = true;
var pictures = ['<i class="fa fa-dribbble"></i></span>',
                '<i class="fa fa-dropbox"></i></span>',
                '<i class="fa fa-twitter"></i></span>',
                '<i class="fa fa-facebook"></i></span>',
                '<i class="fa fa-github"></i></span>',
                '<i class="fa fa-linkedin"></i></span>',
                '<i class="fa fa-youtube"></i></span>',
                '<i class="fa fa-flickr"></i></span>',
                '<i class="fa fa-skype"></i></span>',
                '<i class="fa fa-foursquare"></i></span>',];

function givePic(i){
  return pictures[i];
}

//tiles creation loop
for(var i=0; i<number_of_tiles; i++){
  tiles.push(Math.floor(i/2));
}

//shuffling loop
var swap,tmp;
for(var i=number_of_tiles-1; i>0; i--){
  swap = Math.floor(Math.random()*i);
  tmp = tiles[i];
  tiles[i] = tiles[swap];
  tiles[swap] = tmp;
}

//tile placing loop
for(var i=0; i<number_of_tiles; i++){
  tile = new tile_box;
  var id = Math.floor(Math.random()*300);
  tile.tile_type = tiles[i];
  tile.addToScene(id);
}

function resetGame(){
  alert("Congratulations! You have tried "+tries+" times.");
}

//tile click function
function clicked(){
  if(can_pick){
    var picked = this;
    if(picked_tiles.indexOf(picked) == -1){
      picked_tiles.push(picked);
      picked.innerHTML = givePic(picked.dataset['type']);
    }
    
    if(picked_tiles.length == 2){
      tries++;
      can_pick = false;
      if(picked_tiles[0].dataset['type'] == picked_tiles[1].dataset['type']){
        setTimeout(function(){
          picked_tiles[0].removeEventListener('click',clicked,false);
          picked_tiles[1].removeEventListener('click',clicked,false);
          picked_tiles[0].innerHTML = "";
          picked_tiles[1].innerHTML = "";
          picked_tiles[0].className = picked_tiles[0].className + " removed";
          picked_tiles[1].className = picked_tiles[1].className + " removed";
          picked_tiles = new Array();
          can_pick = true;
          openings++;
          if(openings == (number_of_tiles/2)){
            resetGame();
          }
        },1000);
      } else {
        setTimeout(function(){
          picked_tiles[0].innerHTML = '<i class="fa fa-question"></i>';
          picked_tiles[1].innerHTML = '<i class="fa fa-question"></i>'; 
          picked_tiles = new Array();
          can_pick = true;
        },1000);
      }
    }
  }
}

//add event listeners to tiles
var elements = document.getElementsByTagName("span");
for(var i=0; i<elements.length; i++){
  elements[i].addEventListener('click',clicked);
}
              
            
!
999px

Console