Pen Settings

HTML

CSS

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

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

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.

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

              
                
<div class="grid">
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
  <div class="card"><i class="fas"></i></div>
</div>
<div id="cockpit">
  Versuche: <span id="tries">0</span> | 
  Zeit: <span id="time">00:00</span> 
  <button id="reload">Neustart</button>
</div>
              
            
!

CSS

              
                body {
  background: #ddd;
  font-family: sans-serif;
}

.grid {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 3%;
  grid-auto-rows: 1fr;
  max-width: 600px;
  margin: 2rem auto;
}

.grid::before {
  content: '';
  width: 0;
  padding-bottom: 100%;
  grid-row: 1 / 1;
  grid-column: 1 / 1;
}

.grid > *:first-child {
  grid-row: 1 / 1;
  grid-column: 1 / 1;
}

.grid > div {
  background: green;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  text-align: center;
  font-size: 9vw;
  color: white;
  border: 0.8vw solid white;
  cursor: pointer;

}

div > i {
  position: relative;
  top: calc(3vh);
  min-width: 63px;
}

#cockpit {
  text-align: center;
  margin-top: 6rem;
  font-size: 1.5rem;
  font-weight: bold;
}

@media all and (min-width: 600px) {
  .grid > div {
    font-size: 3.5rem;
    border: 6px solid white;
    }
  div > i {
    top: calc(50% - 33px);
    min-width: 70px;
  }
}

              
            
!

JS

              
                var cards           = document.getElementsByClassName("card");
var triesContainer  = document.getElementById("tries");
var timeContainer   = document.getElementById("time");
var reloadButton    = document.getElementById("reload");

var firstMove = true;
var firstCard;
var tries = 0;
var currentTime;
var open = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var countOpenCards = 0;
var countFoundPairs = 0;
var counterStarted = false;
var counter;

var elements = [
  "fa-car-side",
  "fa-car-side",
  "fa-crow",
  "fa-crow",
  "fa-anchor",
  "fa-anchor",
  "fa-cat",
  "fa-cat",
  "fa-carrot",
  "fa-carrot",
  "fa-bone",
  "fa-bone",
  "fa-birthday-cake",
  "fa-birthday-cake",
  "fa-bomb",
  "fa-bomb"
];

shuffle(elements);

for (let i = 0; i < cards.length; i++) {
  cards[i].addEventListener("click", function(event) {
    // Start counter at first click
    if (!counterStarted) {
      startCounter();
      counterStarted = true;
    }
    // Sicherstellen, dass die angeklicke Karte noch nicht offen ist und dass
    // insgesamt maximal zwei Karten aufgeckt werden (ohne die bereits gefundenen Paare)
    if (!open[i] && countOpenCards < 2) {
      cards[i].firstChild.classList.add(elements[i]); // Karte aufdecken
      open[i] = true; // Merken, dass die Karte aufgedeckt ist
      countOpenCards++; // Zähler der aufgedeckten Karten um eins erhöhen
      // Erste Karte merken
      if (firstMove) {
        firstMove = false;
        firstCard = i;
      } else {
        countOpenCards++; // Zähler der aufgedeckten Karten auf 2 erhöhen
        triesContainer.innerHTML = ++tries; // Versuche um eins erhöhen
        var differentCards = elements[firstCard].localeCompare(elements[i]); // Karten vergleichen
        // Kein Paar: Die Karten nach 1 Sekunde wieder umdrehen
        if (differentCards) {
          setTimeout(function() {
            turnCardsBack(i, firstCard);
          }, 1000);
        } else {
          countOpenCards = 0; // Zähler für aufgedeckte Karten zurücksetzen
          countFoundPairs++; // Zähler für gefundene Paar um eins erhöhen
          // Wenn alle Paare gefunden wurden den Counter anhalten und beglückwünschen
          if (countFoundPairs == 8) {
            stopCounter();
            alert("Super! " + tries + " Versuche in nur " + currentTime);
          }
        }
        firstMove = true; // Nach dem zweiten Zug firstMove wieder auf true setzen
      }
    }
  });
}

reloadButton.addEventListener("click", function() {
  document.location.reload(true);
});

function turnCardsBack(a, b) {
  cards[a].firstChild.classList.remove(elements[a]);
  cards[b].firstChild.classList.remove(elements[b]);
  open[a] = false;
  open[b] = false;
  countOpenCards = 0;
}

// Fisher–Yates shuffle algorithm
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i
    [array[i], array[j]] = [array[j], array[i]]; // swap elements
  }
}

function startCounter() {
  var countDownDate = new Date().getTime();
  counter = setInterval(function() {
    var now = new Date().getTime();
    var distance = now - countDownDate;
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    var secondsLeadingZero;
    var minutesLeadingZero;
    seconds < 10 ? (secondsLeadingZero = "0") : (secondsLeadingZero = "");
    minutes < 10 ? (minutesLeadingZero = "0") : (minutesLeadingZero = "");
    currentTime =
      minutesLeadingZero + minutes + ":" + secondsLeadingZero + seconds;
    document.getElementById("time").innerHTML = currentTime;
  }, 1000);
}

function stopCounter() {
  clearInterval(counter);
}

              
            
!
999px

Console