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

              
                <header>
  <span class="title">memory game</span>

  <div class="buttons">
    <button class="button" id="restart">Restart</button>
    <button class="button" id="new_game">New Game </button>
  </div>
</header>

<div class="details">
  <div class="flips">
    <span class="text">flips</span>
    <span class="dynamic_text" id="num_flips"></span>
  </div>
</div>
<main>
  <div class="row">
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">

      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>

    </div>
  </div>

  <div class="row">
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>

  </div>

  <div class="row">
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>

  </div>

  <div class="row">

    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>
    <div class="card">
      <div class="card_inner">
        <div class="front"></div>
        <div class="back"></div>
      </div>
    </div>

  </div>
</main>

<div class="attribution">Icons made by <a href="https://www.freepik.com" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a>
  <p>Design inspired by <a href="https://www.frontendmentor.io" target="_blank">frontendmentor.io</a></p>
</div>

<!-- modals -->


<div id="success_modal" class="modal">
  <p class="modal_text">You win!</p>
  <button class="button" id="replay">Play Again</button>
</div>

<div id="shield"></div>
              
            
!

CSS

              
                $dark-primary: #000;
$dark-secondary: #14213d;
$success: #FCA311;
$reveal: #E5E5E5;
$light-primary: #fff;

@import url('https://fonts.googleapis.com/css2?family=Creepster&family=Montserrat:wght@100;300;500;600&display=swap');

html,body{
  height:100%;
  overflow:hidden;
  box-sizing: border-box;
  padding:0;
  margin:0;
}
body{
  position: relative;
}
.modal{
  width: 400px;
  height: 200px;
  position:absolute;
  z-index:2;
  left: 50%;
  margin-left: -200px;
  top: 50%;
  margin-top: -100px;
  border-radius: 2rem;
  font-family: 'Creepster', cursive;
  text-align:center;
}
.modal_text{
  font-size:3rem;
  margin:1.5rem;
  text-stroke:1px yellow; 
  -webkit-text-stroke: 1px yellow;
}
#success_modal{
  background: $success;
  display:none;
}

#shield{
  position:fixed;
  width:100%;
  height:100%;
  top:0;
  z-index:1;
  background:black;
  opacity: 0.5;
  display:none;
}


header{
  padding: 2rem;
  display:flex;
  justify-content: space-between;
  margin: 0 2rem;
  align-items:center;
}
.button{
  border:0;
  border-radius: 5rem;
  padding: 0.5rem 1.5rem;
  font-family: 'Montserrat', sans-serif;
}

.button:hover{
  cursor:pointer;
}
#restart{
  background: $success;
  color: $light-primary;
}
#restart:hover{
  background: $dark-secondary;
  color: $success;
}
#new_game, #replay{
  background: $reveal;
  color: $dark-secondary;
}
#new_game:hover, #replay:hover{
  background: $dark-secondary;
  color: $success;
}
.title{
  font-size: 1.7rem;
  font-family: 'Montserrat', sans-serif;
  color: $dark-secondary;
}

.details{
  margin: 0 5rem;
  padding: 0 3rem;
  display:flex;
  justify-content: space-around;
}

.text{
  font-family: 'Montserrat', sans-serif;
  font-size: 1.2rem;
}
.dynamic_text{
   font-family: 'Montserrat', sans-serif;
  font-size: 1.2rem;
  color: #14213d;
}

main{
  display: flex;
  justify-content: center;
  align-items:center;
  margin-top: 0.5rem;
}
.card{
  width: 5em;
  height: 5em;
  background-color: transparent;
  border-radius: 50%;
  margin: 0.5rem;
  text-align:center;
  perspective: 1000px;
}
img{
  width:3em;
}

.card:hover{
  cursor:pointer;
}

.card_inner{
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}
// .card:hover .card_inner{
//   transform: rotateY(180deg);
// }
.front, .back{
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  -webkit-backface-visibility: hidden; /* Safari */
  backface-visibility: hidden;
}
.front{
  background: $dark-secondary;
}
.back{
  transform: rotateY(180deg);
  background: $reveal;
  display:flex;
  justify-content:center;
  align-items:center;
}

.attribution{
  font-family: 'Montserrat', sans-serif;
  font-size: 0.7rem;
  color: gray;
  text-align: center;
  margin-top: 2rem;
}
.attribution a{
  text-decoration: underline;
  color: $success;
}
              
            
!

JS

              
                const cardContents = [
  "lollipop",
  "lollipop",
  "cat",
  "cat",
  "bat",
  "bat",
  "potion",
  "potion",
  "mummy",
  "mummy",
  "pumpkin",
  "pumpkin",
  "ghost",
  "ghost",
  "witch",
  "witch"
];

const referenceTable = [
  {
    name: "lollipop",
    image: "https://i.ibb.co/4V69tNC/lollipop.png"
  },
  {
    name: "cat",
    image: "https://i.ibb.co/wcQK3PT/black-cat.png"
  },
  {
    name: "bat",
    image: "https://i.ibb.co/Wfh0fkz/bat.png"
  },
  {
    name: "potion",
    image: "https://i.ibb.co/3B710fy/potion.png"
  },
  {
    name: "mummy",
    image: "https://i.ibb.co/6PnwXHx/mummy.png"
  },
  {
    name: "pumpkin",
    image: "https://i.ibb.co/PFRwFVg/pumpkin.png"
  },
  {
    name: "ghost",
    image: "https://i.ibb.co/RP4TWh1/ghost.png"
  },
  {
    name: "witch",
    image: "https://i.ibb.co/tchtW0p/witch.png"
  }
];

var winConditionMet = false;
var totalFlips = 0
var cardsFlipped = 0;
const cardList = Array.from(document.querySelectorAll('.card'))
const cards = Array.from(document.getElementsByClassName("back"));

const ogLength = cardContents.length;
const resetBtn = document.getElementById('restart')
const replayBtn = document.getElementById('replay')
const newGame = document.getElementById('new_game')
// pseudocode
// shuffle array contents and divide them amongst the cards
// when a card is clicked reveal it
// keep a count of number of cards revealed
// when another card is clicked reveal it
// if the names of the cards match, turn them orange and leave them flipped, reset number, disable click
// if they dont match turn them back around and reset number


function shuffle(){
  winConditionMet = false;
  totalFlips = 0
document.getElementById('num_flips').innerHTML = totalFlips
  const currentArray = [...cardContents]
  cardList.forEach(card=>{       if(card.classList.contains('flipped') || card.classList.contains('success')){
     
     setTimeout(function(){
card.classList.remove('success')
       card.querySelector(".back").style.backgroundColor = "#E5E5E5"
       card.querySelector('.card_inner').style.transform = "none"   
  card.style.removeProperty('pointer-events')            
    card.classList.remove('flipped')
         
}, 100); 
          }
        })
  for (let i = 0; i < ogLength; i++) {
  var randomElement =
    currentArray[Math.floor(Math.random() * currentArray.length)];

  //we now have a random element
  //look for this element in the reference table
  let relevantObj = referenceTable.find((obj) => obj.name === randomElement);
  let relevantImage = relevantObj.image;
  //   put this image on the back of the card
  cards[i].innerHTML = "<img src=" + relevantImage + "/>";
  //remove this element from the original array we were working with
  let index = currentArray.indexOf(randomElement);
  currentArray.splice(index, 1);
}
}

//lets add a click functionality and

// lets add a flipped count
document.querySelectorAll(".card").forEach((item) => {
  item.addEventListener("click", () => {
    let child = item.querySelector(".card_inner");
    if (cardsFlipped < 2) {
      totalFlips++
      document.getElementById('num_flips').innerHTML = totalFlips
      child.style.transform = "rotateY(180deg)";
      item.classList.add("flipped");
    }
    if (cardsFlipped == 1) {
      //compare and reset
      let currentFlipped = document.querySelectorAll(".flipped");
      //time to compare
      const first = currentFlipped[0].querySelector("img").getAttribute("src");
      const second = currentFlipped[1].querySelector("img").getAttribute("src");
      if (first === second) {
        //match!
        currentFlipped.forEach((current) => {
         setTimeout(function(){
            current.querySelector(".back").style.backgroundColor = "#FCA311";
         },150)
          current.style.pointerEvents = "none";
          current.classList.remove("flipped");
          current.classList.add("success")
          
          

          //reset
          cardsFlipped = -1;
        });
      }else if(first !== second){
        //hide all the cards again
        cardsFlipped = -1
        //take all the cards
        let cardlist = document.querySelectorAll('.card')
        cardlist.forEach(card=>{       if(card.classList.contains('flipped')){
     setTimeout(function(){card.querySelector('.card_inner').style.transform = "none"
         
}, 300); card.classList.remove('flipped')
          }
        })
        
      }
      
    }
    
    cardsFlipped++;
    
//have all the cards been found?
          let successfulFlips = Array.from(document.querySelectorAll('.success'))
          if(successfulFlips.length === ogLength){
  winConditionMet = true;          document.getElementById('shield').style.display = 'block'
            document.getElementById('success_modal').style.display='block'
          }
                                     
  });
});


  resetBtn.addEventListener('click',()=>{
    shuffle()
  })

replayBtn.addEventListener('click',()=>{ document.getElementById('shield').style.display = 'none'
            document.getElementById('success_modal').style.display='none'
      
                                        shuffle()
  
})

newGame.addEventListener('click',()=>{

  shuffle()
})

//on page load
shuffle()
              
            
!
999px

Console