123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
<head>
  <meta charset="utf-8">
  <title>Memory Game</title>
  <link rel="stylesheet" href="css/app.css"/>
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
</head>
<body>
   <nav class="navbar navbar-inverse navbar-fixed-top">
   <div class="container-fluid">
     <div class="navbar-header">
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span> 
      </button>
       <a class="navbar-brand href="#top"" id="top"> Dean</a>
     </div>
       
     <ul class="nav navbar-nav navbar-right">
       <li class="activate"> <a href="index.html"> ABOUT </a> </li>
       <li> <a href="#top"> CARDS GAME </a> </li>
       <li><a href="index.html"> CONTACT</a></li> </ul>
       </div>
     </nav>
  <!--Main grid-->
  <br><br>
    <header class="box">
        <img src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064622/logo_jbdzfk.png" class="logo" />
        <h1>Memory Monsters</h1>
        <h2>Where we <span class="niceText">Scare</span> people Off
   <button id="foo" type="button" class="btn" onclick="shuffle();">
          Restart Game!</button>
 <button type="button" value="click" class="btn" onclick="shuffle();">Shuffle</button></div> 
          </h2>
    </header>
    <div class="board" id="main">
      
        <div class="card" data-card="1" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/1_plrmj0.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="2" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/2_bykxuj.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="1" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/1_plrmj0.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="2" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/2_bykxuj.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="3" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/3_wgtknx.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="4" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064602/4_jg6e9r.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="3" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064604/3_wgtknx.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="4" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064602/4_jg6e9r.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="5" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064602/5_ttd5mn.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="6" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/6_yk8qwj.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="5" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064602/5_ttd5mn.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>
        <div class="card" data-card="6" onclick="cardClicked(this)">
            <img class="front" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/6_yk8qwj.png">
            <img class="back" src="https://res.cloudinary.com/dy4voxduv/image/upload/v1468064603/back_e4mcye.png" >
        </div>

    </div>


  <script src="js/app.js"></script>
 
</body>
</html>

            
          
!
            
              header {
    background-color: black;
    padding:10px;
    margin-bottom: 0px;
}
.navbar {
  position: realtive;
  margin-bottom: 200px;
}

h1 {
    color: white !important;
    font-size: 4em;
}
h2 {
    color: white !important;
}

.niceText {
    font-style: italic;
    color:lightgreen;
}

.btn {
 display: flex; 
justify-content: center;
margin-left: 100px;
color: black;
}
.logo {
position: absolute;
width: 130x;
height: 130px;
right: 20px;
z-index:999;
}



.card {
    width:170px;
    height:192px;
    float:left;
    margin: 10px;
}

.card img {
    position: absolute;
}

.card.flipped .back{
    display:none;
}

#main:after {
  content: "";
  display: table;
  clear: both;
}
#main {
   margin-top: 0px;
   background-color: lightgray;
}
.front {
  width: 140px;
}

            
          
!
            
              // Those are global variables, they stay alive and reflect the state of the game
var elPreviousCard = null;
var flippedCouplesCount = 0;
var cards_testing = 0;

// This is a constant that we dont change during the game (we mark those with CAPITAL letters)
var TOTAL_COUPLES_COUNT = 6;

// Load an audio file win + right + wrong
var audioWin = new Audio('sound/win.mp3');

var audioRight = new Audio('sound/right.mp3');

var audioWrong = new Audio('sound/wrong.mp3');

// shuffle + reset game  flipped cards off
function shuffle(){
  // select all cards and runs loop to shuffle the order
var board = document.querySelector('.board');
for (var i = board.children.length; i >= 0; i--) {
board.appendChild(board.children[Math.random() * i | 0]);

    // Get all DOM object with class "flipped". as its what was used for displaying "hidden" image.
    var flipped_cards = document.getElementsByClassName('flipped');
    // Loops through all flipped_cards and flip them back by toggling class "flipped" off
    // already initialized i in this function block. So no need for var i.
    for(i = 0; i < flipped_cards.length; i++){
        // classList.toggle. adds class if element does not have it and removes it if elements have it.
        flipped_cards[i].classList.toggle('flipped');
    }
  }
}
// This function is called whenever the user click a card
function cardClicked(elCard) {
  // main scope, Only 2 card can be visible at same time.
if(cards_testing < 2){
    // This card will be visible. So one more card visible in counter.
    cards_testing++;
  
    // If the user clicked an already flipped card - do nothing and return from the function
    if (elCard.classList.contains('flipped')) {
        return;
    }

    // Flip it
    elCard.classList.add('flipped');

    // This is a first card, only keep it in the global variable
    if (elPreviousCard === null) {
        elPreviousCard = elCard;
    } else {
        // get the data-card attribute's value from both cards
        var card1 = elPreviousCard.getAttribute('data-card');
        var card2 = elCard.getAttribute('data-card');

        // No match, schedule to flip them back in 1 second
        if (card1 !== card2){
            setTimeout(function () {
              // Hides this card again. So now two less card visible in counter.
                cards_testing -= 2;
                elCard.classList.remove('flipped');
                elPreviousCard.classList.remove('flipped');
                elPreviousCard = null;
            }, 1000)
            // Play music for a mistake
            AudioWrong.play();
        } 
      else {
            // Yes! a match!
          // Both card were correct. No need to hide both. Reset testing counter.
          cards_testing = 0;
          flippedCouplesCount++;
          elPreviousCard = null;
          audioRight.play();

            // All cards flipped!
            if (TOTAL_COUPLES_COUNT === flippedCouplesCount) {
                audioWin.play();
            
             }
  
         }

      }
  }

}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console