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

              
                <!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Matching Game</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
    <link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Coda">
    <link rel="stylesheet" href="css/app.css">
</head>
<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="js/app.js"></script>

    <div class="container">
        <header>
            <h1>Matching Game</h1>
        </header>

        <section class="score-panel">
          <div class="score stars-container">
        	   <ul class="stars">
        		     <li><i class="fa fa-star"></i></li>
        		     <li><i class="fa fa-star"></i></li>
        		     <li><i class="fa fa-star"></i></li>
        	   </ul>
          </div>

          <div class="score timing">
            <span class="time">Time</span> <span class="clock">00:00</span>
          </div>

          <div class="score moves-score">
        	   <span class="moves">0</span><span class="movesWord">Moves</span>
          </div>

          <div class="score restart">
        		    <i class="fa fa-repeat"></i>
        	</div>

        </section>

        <ul class="deck">
            <li class="card">
                <i class="fa fa-diamond"></i>
            </li>
            <li class="card">
                <i class="fa fa-paper-plane-o"></i>
            </li>
            <li class="card">
                <i class="fa fa-flask"></i>
            </li>
            <li class="card">
                <i class="fa fa-bolt"></i>
            </li>
            <li class="card">
                <i class="fa fa-cube"></i>
            </li>
            <li class="card">
                <i class="fa fa-fort-awesome"></i>
            </li>
            <li class="card">
                <i class="fa fa-birthday-cake"></i>
            </li>
            <li class="card">
                <i class="fa fa-flask"></i>
            </li>
            <li class="card">
                <i class="fa fa-diamond"></i>
            </li>
            <li class="card">
                <i class="fa fa-bomb"></i>
            </li>
            <li class="card">
                <i class="fa fa-birthday-cake"></i>
            </li>
            <li class="card">
                <i class="fa fa-bomb"></i>
            </li>
            <li class="card">
                <i class="fa fa-bolt"></i>
            </li>
            <li class="card">
                <i class="fa fa-fort-awesome"></i>
            </li>
            <li class="card">
                <i class="fa fa-paper-plane-o"></i>
            </li>
            <li class="card">
                <i class="fa fa-cube"></i>
            </li>
        </ul>
    </div>

    <div id="winner" class="overlay">
      <div class="popup">
        <p class="header">Congratulations!</p>
        <div class="final-rating">
          <span id="star-rating"></span>
        </div>
        <div class="message">
          <p>You won in <span id="final-move"></span> moves </p>
          <p>in <span id="total-time"></span>!</p>
        </div>
        <button class="play-again">Play Again</button>
      </div>
    </div>



</body>
</html>

              
            
!

CSS

              
                html {
    box-sizing: border-box;
}

*,
*::before,
*::after {
    box-sizing: inherit;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    background: #ffffff url('https://image.ibb.co/jSbD9y/geometry2.png'); /* Background pattern from Subtle Patterns */
    font-family: 'Coda', cursive;
}

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

h1 {
    font-family: 'Open Sans', sans-serif;
    font-weight: 600;
    color: #aa7ecd;
}

/*
 * Styles for the deck of cards
 */

.deck {
    width: 660px;
    height: 680px;
    background: linear-gradient(160deg, #02ccba 0%, #aa7ecd 100%);
    padding: 32px;
    border-radius: 10px;
    box-shadow: 0px 10px 18px 1px rgba(46, 61, 73, 0.5);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    margin: 0 0 3em;
}

.deck .card {
    height: 125px;
    width: 125px;
    background: #2e3d49;
    font-size: 0;
    color: #ffffff;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 5px 2px 20px 0 rgba(46, 61, 73, 0.5);
}

.deck .card.open {
    transform: rotateY(0);
    background: #02b3e4;
    cursor: default;
    animation-name: cardFlip;
    -webkit-backface-visibility: visible !important;
    backface-visibility: visible !important;
    animation-duration: .75s;
}

.deck .card.show {
    font-size: 33px;
}

.deck .card.match {
    cursor: default;
    background: #aa7ecd;
    font-size: 33px;
    pointer-events: none;
    animation-name: jiggle;
    -webkit-backface-visibility: visible !important;
    backface-visibility: visible !important;
    animation-duration: .75s;
}

.deck .card.lock {
  pointer-events: none;
}

.deck .card.unmatch {
  background: red;
  font-size: 33px;
  animation-name: shake;
	-webkit-backface-visibility: visible !important;
	backface-visibility: visible !important;
	animation-duration: .75s;
}

/*
 * Styles for the Score Panel
 */

.score-panel {
    text-align: center;
    width: 650px;
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    font-size: 20px;
    vertical-align: middle;
}

.score-panel .stars {
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: row;
    margin: 0 5px 0 0;
}

.score-panel .stars li {
    list-style: none;
    display: inline-block;
    margin-left: 2px;
    margin-right: 2px;
}

.score-panel .restart {
    align-self: right;
    cursor: pointer;
}

.score {
  margin-left: 40px;
  margin-right: 40px;
  align-items: center;
  vertical-align: middle;
  text-align: center;
}

.time {
  font-weight: 700;
  color: #02ccba;
  padding-right: 10px;
}

.movesWord {
  font-weight: 700;
  color: #02ccba;
  padding-left: 10px;
}

.fa-star {
  font-size: 40px;
  color: #aa7ecd;
}

.timing, .moves-score, .restart{
  padding-top: 10px;
}

/*
 * Styles for the Winner Popup
 */

.overlay {
  position: fixed;
  top: 25%;
  left: 25%;
  width: 50%;
  background: #E3FFFD;
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
  align-items: center;
  border-radius: 15px;
  box-shadow: 0px 0px 18px 1px rgba(0, 0, 0, 0.35);
}

.overlay:target{
  visibility: visible;
  opacity: 1;
}

.popup {
  border-radius: 15px;
  background: #ffffff;
  position: relative;
  transition: all 5s ease-in-out;
  font-family: arial;
  align-items: center;
  vertical-align: middle;
  display: flex;
  flex-direction: column;
  padding-top: 12%;
  padding-bottom: 12%;
}

.header {
  align-items: center;
  font-family: arial;
  font-size: 30px;
  font-weight: bold;
  -webkit-margin-before: 0;
}

.popup .final-rating,
.message {
  max-height: 30%;
  overflow: auto;
  text-align: center;
}

.final-move, .total-time {
  color: #02ccba;
}

.show {
  visibility: visible !important;
  opacity: 1 !important;
}

#star-rating li {
  display: inline-block;
  font-size: 40px;
  color: #aa7ecd;
}

.play-again {
  background-color: #02ccba;
  font-size: 14px;
  display: block;
  margin: 0 auto;
  width: 30%;
  font-family: Arial;
  color: black;
  border-radius: 5px;
  margin-top: 15px;
  padding-top: 15px;
  padding-bottom: 15px;
  font-weight: bold;
  box-shadow: 3px 3px 3px 0px rgba(46, 61, 73, 0.5);
  border-color: #02ccba;
}

/* Animations */
@keyframes cardFlip {
  from {
    transform: perspective(400px) rotate3d(0, 1, 0, 90deg);
    animation-timing-function: ease-in;
  }

  40% {
    transform: perspective(400px) rotate3d(0, 1, 0, -20deg);
    animation-timing-function: ease-in;
  }

  60% {
    transform: perspective(400px) rotate3d(0, 1, 0, 10deg);
  }

  80% {
    transform: perspective(400px) rotate3d(0, 1, 0, -5deg);
  }

  to {
    transform: perspective(400px);
  }
}

@keyframes jiggle {
  from {
		transform: scale3d(1, 1, 1);
	}

	30% {
		transform: scale3d(1.25, 0.75, 1);
	}

	40% {
		transform: scale3d(0.75, 1.25, 1);
	}

	50% {
		transform: scale3d(1.15, 0.85, 1);
	}

	65% {
		transform: scale3d(.95, 1.05, 1);
	}

	75% {
		transform: scale3d(1.05, .95, 1);
	}

	to {
		transform: scale3d(1, 1, 1);
	}
}

@keyframes shake {
  from {
    transform: perspective(400px) rotate3d(0, 0, 1, 20deg);
  }

  40% {
    transform: perspective(400px) rotate3d(0, 0, 1, -15deg);
  }

  60% {
    transform: perspective(400px) rotate3d(0, 0, 1, 10deg);
  }

  80% {
    transform: perspective(400px) rotate3d(0, 0, 1 -5deg);
  }

  to {
    transform: perspective(400px);
  }
}

/****** Media queries******/

/****** Phone******/
@media screen and (max-width: 320px) {
	.deck {
		width: 300px;
    height: 300px;
    padding: 5px 5px 5px 5px;
	}

	.deck .card {
		height: 60px;
		width: 60px;
    display: flex;
    flex-wrap: wrap;
    padding: 5px 5px 5px 5px;
	}

  .score-panel {
      text-align: center;
      width: 300px;
      margin-bottom: 10px;
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      font-size: 14px;
      vertical-align: middle;
  }

  .score-panel .stars {
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: row;
      margin: 0 5px 0 0;
  }

  .score-panel .stars li {
      list-style: none;
      display: inline-block;
      margin-left: 2px;
      margin-right: 2px;
  }

  .score-panel .restart {
      align-self: right;
      cursor: pointer;
  }

  .stars-container {
    order: 0;
  }

  .restart {
    order: 1;
  }

  .timing {
    order: 2;
  }

  .moves-score{
    order: 3;
  }

  .score {
    align-items: center;
    vertical-align: middle;
    text-align: center;
  }

  .time {
    font-weight: 700;
    color: #02ccba;
    padding-right: 10px;
  }

  .movesWord {
    font-weight: 700;
    color: #02ccba;
    padding-left: 10px;
  }

  .fa-star {
    font-size: 20px;
    color: #aa7ecd;
  }

  .header {
    font-size: 20px;
  }

  .play-again {
    background-color: #02ccba;
    font-size: 14px;
    display: block;
    margin: 0 auto;
    width: inherit;
    font-family: Arial;
    color: black;
    border-radius: 5px;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    font-weight: bold;
    box-shadow: 3px 3px 3px 0px rgba(46, 61, 73, 0.5);
    border-color: #02ccba;
  }

  .overlay {
    width: 80%;
    padding: 10px 10px 10px 10px;
    top: 10%;
    left: 10%;
  }
}


/****** tablets******/

@media screen and (min-width: 321px) and (max-width: 720px) {
	.container {
		font-size: 22px;
	}

  .deck {
		width: 400px;
    height: 400px;
    padding: 20px 20px 20px 20px;
	}

	.deck .card {
		height: 80px;
		width: 80px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px 10px 10px 10px;
	}

  .score-panel {
      text-align: center;
      width: 400px;
      margin-bottom: 10px;
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      font-size: 14px;
      vertical-align: middle;
  }

  .score-panel .stars {
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: row;
      margin: 0 5px 0 0;
  }

  .score-panel .stars li {
      list-style: none;
      display: inline-block;
      margin-left: 2px;
      margin-right: 2px;
  }

  .score-panel .restart {
      align-self: right;
      cursor: pointer;
  }

  .stars-container {
    order: 0;
  }

  .restart {
    order: 3;
  }

  .timing {
    order: 1;
  }

  .moves-score{
    order: 2;
  }

  .score {
    align-items: center;
    vertical-align: middle;
    text-align: center;
    margin-left: 20px;
    margin-right: 20px;
  }

  .time {
    font-weight: 700;
    color: #02ccba;
    padding-right: 10px;
  }

  .movesWord {
    font-weight: 700;
    color: #02ccba;
    padding-left: 10px;
  }

  .fa-star {
    font-size: 20px;
    color: #aa7ecd;
  }

  .header {
    font-size: 20px;
  }

  .play-again {
    background-color: #02ccba;
    font-size: 14px;
    display: block;
    margin: 0 auto;
    width: inherit;
    font-family: Arial;
    color: black;
    border-radius: 5px;
    margin-top: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
    font-weight: bold;
    box-shadow: 3px 3px 3px 0px rgba(46, 61, 73, 0.5);
    border-color: #02ccba;
  }

  .overlay {
    width: 80%;
    padding: 10px 10px 10px 10px;
    top: 10%;
    left: 10%;
  }
}

              
            
!

JS

              
                $(document).ready(function() {

/*
 * Create a list that holds all of your cards
 */
var cards = $('.card').get();

/*
 * Display the cards on the page
 *   - shuffle the list of cards using the provided "shuffle" method below
 *   - loop through each card and create its HTML
 *   - add each card's HTML to the page
 */

 shuffle(cards);

 const deck = document.querySelector(".deck");
 for (const card of cards) {
   deck.appendChild(card);
 };

// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;

    while (currentIndex !== 0) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}


/*
 * set up the event listener for a card. If a card is clicked:
 *  - display the card's symbol (put this functionality in another function that you call from this one)
 *  - add the card to a *list* of "open" cards (put this functionality in another function that you call from this one)
 *  - if the list already has another card, check to see if the two cards match
 *    + if the cards do match, lock the cards in the open position (put this functionality in another function that you call from this one)
 *    + if the cards do not match, remove the cards from the list and hide the card's symbol (put this functionality in another function that you call from this one)
 *    + increment the move counter and display it on the page (put this functionality in another function that you call from this one)
 *    + if all cards have matched, display a message with the final score (put this functionality in another function that you call from this one)
 */

 var openCardList = [];

 for (const card of cards) {
   card.addEventListener("click", function() {
     clockRunning = true;
     if (openCardList.length < 2) {
       listOpenCards(card);
       show(card);
     };

     if (openCardList.length == 2) {
       lockAllCards();
       checkMatch();
       setTimeout( function() {
       unlockAllCards()},750);
     }

   });
 };

function show(card) {
   $(card).addClass('open');
   $(card).addClass('show');
   $(card).addClass('lock');
 }

var moves = 0;
var counter = $('.moves').get()[0];

function increaseCounter() {
  moves++;
  counter.innerHTML = moves;
}

 function listOpenCards(card) {
   openCardList.push(card);
 }

function hide(card) {
  $(card).removeClass('open');
  $(card).removeClass('show');
  $(card).removeClass('lock');
}

var matched = 0;

function lockAllCards() {
  for (card of cards) {
    $(card).addClass('lock');
  };
}

function unlockAllCards() {
  for (card of cards) {
    $(card).removeClass('lock');
  };
}

 function checkMatch() {
   increaseCounter();
   hideStar();
   if (openCardList[0].innerHTML == openCardList[1].innerHTML) {
     matched++;
     $(openCardList[0]).addClass('match');
     $(openCardList[1]).addClass('match');
     openCardList = [];
   } else {
     $(openCardList[0]).addClass('unmatch');
     $(openCardList[1]).addClass('unmatch');
     setTimeout( function() {
       for (card of openCardList) {
         $(card).removeClass('unmatch');
         hide(card);
       };
       openCardList = [];
     },1100);
   };
   if (matched == 8) {
     success();
   };
 }

 //restart function that restarts the counter, shuffles the cards, and resets the board
 function restart() {
   moves = 0;
   counter.innerHTML = 0;
   matched = 0;
   cards.forEach(function(card) {
     hide(card);
     $(card).removeClass('match');
   });
   shuffle(cards);
   openCardList = [];
   for (const card of cards) {
     deck.appendChild(card);
   };
   clockRunning = false;
   elapsedTime = 0;
   clearInterval(clockInterval);
   clock();
   for (star of stars) {
     star.style.visibility = "visible";
   };
   removedStar=2;
 }

//adds an event listener to the restart div
$('.fa-repeat').get()[0].addEventListener("click", function(){
  restart();
});

 //setting the functionality of the clock, the clock is tied to the evenlistener on the cards above
 var clockRunning = false;
 var elapsedTime = 0;
 var clockDisplay = $('.clock').get()[0];
 var clockInterval;

 //start the clock once the page loads-- prevents weird interval interference
 clock();

 function clock() {
     clockInterval = setInterval (function() {
       if (clockRunning) {
         elapsedTime++;
       };
       var minutes = String(Math.floor(elapsedTime / 60));
       var seconds = String(elapsedTime % 60).padStart(2,'0');
       clockDisplay.innerHTML = minutes + ':' + seconds;
     }, 1000);
 }

 //adding the functionality of the scoring system based on the number of stars displayed
 var stars = $('.fa-star').get();
 var removedStar = 2;

 function hideStar() {
   if (moves == 16 || moves == 24) {
     stars[removedStar].style.visibility = "collapse";
     removedStar--;
   };
 }

 //congratulations modal that reveals the time, star rating, and number of moves utilized to win the Game
 var modal = document.getElementById("winner");

 function success() {
   clockRunning = false;
   clearInterval(clockInterval);
   finalTime = clockDisplay.innerHTML;

   //revealing the congratulations modal upon winning
   modal.classList.add("show");

   //defining star rating variable
   var starRating = document.querySelector(".stars").innerHTML;

   //display the number of moves, the star rating, and time on the Congratulations modal
   document.getElementById("star-rating").innerHTML = starRating;
   document.getElementById("final-move").innerHTML = moves;
   document.getElementById("total-time").innerHTML = finalTime;
 }


 $(".play-again").get()[0].addEventListener("click", function() {
   modal.classList.remove("show");
   restart();
 });

});

              
            
!
999px

Console