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>
<head>
	<meta charset="utf-8">
	<title>Memory Game</title>
	<link rel="stylesheet" href="css/styles.css">
</head>
<body>

	<div id="memoryGame">
		<div class="deck">
			<img class="card" data-card="1" data-reveal="img/1.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/2.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/3.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/4.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/5.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/6.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/7.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/8.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/1.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/2.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/3.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/4.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/5.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/6.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/7.png" src="img/cover.png" >
			<img class="card" data-card="1" data-reveal="img/8.png" src="img/cover.png" >
		</div>
		<img class="title" src="img/title.png">
		<div class="feedback">PICK TWO</div>
		<img class="reset" src="img/button_reset.png">
	</div>


	<script src="js/jquery.min.js"></script>
	<script src="js/randomize.js"></script>
	<script src="js/scripts.js"></script>
</body>
</html>

            
          
!
            
              body {
	background-image: url("../img/background.png");
	background-repeat: no-repeat;
	background-size: 1572px 881px;

	/*width: 1000px;*/
	/*font-family: Helvetica;
	color:#000000;
	text-align:center;*/
}

/*-------------------------------------
| Memory Game Styles
-------------------------------------*/
#memoryGame {
	/*background-color: white;*/
	width: 1300px;
	margin: 50px auto;
	padding: 25px;

	position: relative;
}
#memoryGame .deck {
	width: 700px;
	margin-top: -66px;

	display: flex;
	justify-content: space-between;
	flex-wrap: wrap;
}
#memoryGame .deck img {
	/*box-sizing: border-box;*/
	padding: 10px;
	width: 121px;
	height: 156px;
}
#memoryGame .feedback {
	position: absolute;
	right: 165px;
	top: 380px;
	width: 250px;
	background-color: white;
	border: yellow;

	font-size: 20px;
	color: teal;

	height: 100px;

	display: flex;

	justify-content: center;
	align-items: center;

	font-size: 30px;
	font-weight: bold;
}
#memoryGame .title{
	position: absolute;
	top: 30px;
	right: 60px;
	width: 450px;
	height: 250px;

}
#memoryGame .reset {
	display: block;
	position: absolute;
	top: 561px;
	right: 213px;
	width: 159px;
	height: 43px;
}

            
          
!
            
              /*----------
variables
------------*/

var number_of_pairs_matched =0;
var game_state = 'no card chosen';
var card_numbers_array =[
  1, 2, 3, 4, 5, 6, 7, 8,
  1, 2, 3, 4, 5, 6, 7, 8
];
var firstCrd = '';
/*--------
actions or functions (method)
----------*/
/* 1.when page loads ------------*/
//declaring function
function when_page_loads () {
  console.log('page loaded');
  shuffel_and_deal_cards();//function call
}

//calling the function
when_page_loads();


/*2. shuffel and deal cards----*/
//decraring
function shuffel_and_deal_cards() {
   console.log('shuffel and deal');


//randomize arrey
console.log(card_numbers_array);
card_numbers_array = randomize (card_numbers_array);
console.log(card_numbers_array);



//replace images in document
$(card_numbers_array).each(function(index){
var newImage = 'img/' + card_numbers_array[index] + '.png';
$('.deck img:eq('+index+')').attr('data-reveal', newImage);
});

//reset match count to zero
number_of_pairs_matched = 0;
$('.deck img') .attr('src', 'img/cover.png');
update_user_feedback('Pick Two');

}

//3. update user feedback
function update_user_feedback(message) {
  $('.feedback').html(message);

}


//4.clicking on a card
function click_on_card() {
  console.log(game_state);
//if first card is choosen, app waits for second card
  if (game_state == 'no card chosen')
  {
    game_state = 'one card chosen';
    firstCard = $(this);
    console.log(firstCard);

    $(this).off('click');//temporarly disabled click events

    firstCard.attr('src', firstCard.attr('data-reveal'));
  }

  //if second card choosen, app checks to see if they are a match
  else if (game_state == 'one card chosen')
  {
    game_state = 'two cards chosen';
    console.log('do the stuff if 2 cards are chosen');


    var card1_reveal = firstCard.attr('data-reveal');
    var card2_reveal = $(this).attr('data-reveal');

    $(this).attr('src', $(this).attr('data-reveal'));


    $(this).off('click');//temporarly disabled click events

  //compare reveal to see if they match or number_of_pairs_matched
    //if No match,
    if (card1_reveal != card2_reveal)
    {
      //hide both cards
      var secondCard = $(this); //grabs second card
      setTimeout(function(){
         firstCard.attr('src', 'img/cover.png');
         secondCard.attr('src', 'img/cover.png');
         game_state = 'no card chosen'

        firstCard.click(click_on_card);
        secondCard.click(click_on_card);

       }, 1000);

      //update user feedback to read "sorry no match"
      update_user_feedback("Dang It! Try Again.");
    }
    else   //if match
    {

        //leave chosen displayed
        //update user feedback to read "ypu found the match"
        update_user_feedback("WhoHoo!!!");

        //chack if you won the game
        check_if_they_won();

      }

  }
}

$('.deck img').click(click_on_card);


/*5.  check if user won game------*/

function check_if_they_won() {
  //incraments
  number_of_pairs_matched = number_of_pairs_matched + 1;

  if (number_of_pairs_matched  ==8)
  {
    update_user_feedback("WhoHoo, Winner!");
  }
  else
  {
    firstCard = '';
    game_state = 'no card chosen';
  }
}

/*6. reset button----*/
function reset_game() {
  shuffel_and_deal_cards();
}
$('.reset').click(reset_game);

            
          
!
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