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

              
                
<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

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

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

let 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
         // Paar gefunden
        if (elements[firstCard] === elements[i]) {
          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);
          }
        } else { 
          // Kein Paar: Die Karten nach 1 Sekunde wieder umdrehen
          setTimeout(function () {
            turnCardsBack(i, firstCard);
          }, 1000);
        }
        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() {
  let countDownDate = new Date().getTime();
  counter = setInterval(function () {
    let now = new Date().getTime();
    let distance = now - countDownDate;
    let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    let seconds = Math.floor((distance % (1000 * 60)) / 1000);
    let secondsLeadingZero;
    let 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