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.

            
              <!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>My App</title>
  <link href="https://fonts.googleapis.com/css?family=Lakki+Reddy&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="css/game.css" />
</head>

<body>
  <div class="header">
    <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381646/8_ncxngl.png">
    <h1>Memory Monsters</h1>
  </div>
  <h2 class="userName">Player</h2>
  <h3 class="timeBoard">00:00</h3>
  <div class="board">
      <div class="card" data-card="1" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381475/1_ysnybe.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
        <div class="card" data-card="7" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381397/7_vlgode.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
        <div class="card" data-card="1" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381475/1_ysnybe.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
        <div class="card" data-card="7" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381397/7_vlgode.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
        <div class="card" data-card="5" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381562/2_r7w0iu.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
        <div class="card" data-card="5" onclick="cardClicked(this)">
          <img src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381562/2_r7w0iu.png">
          <img class="back" src="https://res.cloudinary.com/doghchqxw/image/upload/v1573381277/back_eehzbz.png">
        </div>
  </div>
  
  <button class="again" id="button">Play Again</button>
  <button onclick="location.href='index.html'">Home Page</button>
  <button onclick="changeUser()">Change User</button>
  <script src="js/game.js"></script>
</body>

</html>
            
          
!
            
              body {
    padding: 4% 10%;
}
.header {
    background-color: #9ab3e5;
    padding: 20px;
    border: 5px dotted #7194da;
    color:#1f3d7a;
    font-size: 2em;
    text-align: center;
    border-radius: 20px;
    
}
h1 {
    font-family: Lakki Reddy;
    font-size: 5rem;
}
.header img {
    float:right;
}

.card {
    background-color: pink;
    height: 165px;
    width: 165px;    
    float: left;
    margin: 5px;
}

.card img {
    position: absolute;
}

.flipped .back {
    display: none;
}
button {
    padding: 15px;
    font-size: 20px;
    border-radius: 10px;
    background-color: #1f3d7a;
    color: #9ab3e5;
    font-family: Lakki Reddy;
}
.timeBoard {
    font-size: 30px;
    color: gray;
}

            
          
!
            
              // Those are global variables, they stay alive and reflect the state of the game
var flippedCouplesCount = 0;
let currentPlay = [];
// This is a constant that we dont change during the game (we mark those with CAPITAL letters)
var TOTAL_COUPLES_COUNT = 3;

// Load an audio file
var audioWin = new Audio("sound/win.mp3");
var audioWrong = new Audio("sound/wrong.mp3");
var audioRight = new Audio("sound/right.mp3");

var cards = document.querySelectorAll("div");
var isProcessing = false;

function shuffleCards() {
  var board = document.querySelector(".board");
  for (var i = board.children.length; i >= 0; i--) {
    board.appendChild(board.children[(Math.random() * i) | 0]);
  }
}

function changeUser() {
  var userName = prompt("Enter a user name", "Your user name here");
  if (userName != null) {
    document.querySelector("h2").innerText = "User: " + userName;
  }
}
var userName = "s";
document.querySelector("h2").innerText = "User: " + userName;

localStorage.setItem("userName", "Nina");
var userName = localStorage.getItem("userName");
//alert(userName);

function dontShowButton() {
  document.getElementById("button").style.display = "none";
}

window.onload = function() {
  document.getElementById("button").style.display = "none";
};
function showButton() {
  document.getElementById("button").style.display = "block";
  button.addEventListener("click", function() {
    for (i = 0; i < cards.length; i++) {
      cards[i].classList.remove("flipped");
      if (cards.classList == "flipped");
      // dontShowButton();
      shuffleCards();
    }
  });
}
// This function is called whenever the user click a card
function cardClicked(elCard) {
  if (currentPlay.length === 2) {
    return;
  }

  // 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");
  currentPlay.push(elCard);

  if (currentPlay.length === 2) {
    console.debug("El:", currentPlay[0]);
    const card1 = currentPlay[0].dataset.card;
    const card2 = currentPlay[1].dataset.card;

    if (card1 != card2) {
      setTimeout(function() {
        currentPlay[0].classList.remove("flipped");
        currentPlay[1].classList.remove("flipped");
        audioWrong.play();

        currentPlay = [];
      }, 1000);
    } else {
      flippedCouplesCount++;
      audioRight.play();

      currentPlay = [];

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

            
          
!
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