css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

    <header style="text-align: center;">
        <h1>Memory Games</h1>

    <nav style="display: flex; flex-direction: row">
        <h2>Select a difficulty:</h2>
                <button id="start-easy">Easy</button>
                <button id="start-medium">Medium</button>
                <button id="start-hard">Hard</button>

    <section id="stage">


<template id="card-template">
    <article class="card">
        <h2 data-value></h2>

              #stage {
            display: flex;

        .card {
            margin: 1rem;
            width: 10rem;
            height: 10rem;

.card.selected {
            border: blue 2px solid;
            color: blue;
              document.addEventListener('DOMContentLoaded', matchingGame);
        function matchingGame(){

            var startEasy = document.getElementById('start-easy');
            var startMedium = document.getElementById('start-medium');
            var startHard = document.getElementById('start-hard');

            var stage = document.getElementById('stage');

            var cardTemplate = document.getElementById('card-template');

            var totalCards;
            var renderedCards;

            var selectedCard;

            function init(){
                startEasy.addEventListener('click', initEasy);
                // startMedium.addEventListener('click', initMedium);
                // startHard.addEventListener('click', initHard);

            function initEasy(){
                totalCards = 4;// TODO random!
                renderedCards = 0;


            function renderCards(){
                var documentFragments = document.createDocumentFragment();
                var cards = [];
                while(renderedCards < totalCards){
                    var number = getRandomInt(1, 100);
                    // Make two cards
                    var card = renderCard(number);
                    card = renderCard(number);
                    renderedCards += 2;

                cards = shuffle(cards);

                stage.innerHTML = '';

                // TODO bind to the cards and listen for click. Leave the first card 'selected', when the second card is selected, test if htey match and remove them from the sets.

             * @param {int} number
             * @return Node
            function renderCard(number){
                var clone = document.importNode(cardTemplate.content, true);
                var element = clone.querySelector('[data-value]');
                element.dataset.value = number;
                element.innerHTML = number;
                return clone;

            function bindCards(){
                    card.addEventListener('click', handleCardClick);

             * Resets if the game is beaten or not
            function resetSelections(){
                selectedCard = undefined;

             * @param {Event} event
            function handleCardClick(event){
                   console.log('First selection: ', event.currentTarget);
                   selectedCard = event.currentTarget;

               console.log('second selection', selectedCard, event.currentTarget);

               // TODO was this first or second selection?
                // TODO how do you 'show' the user?

             * Reorders an array
             * @param array
             * @returns {*}
            function shuffle(array) {
                var currentIndex = array.length, temporaryValue, randomIndex;

                // While there remain elements to shuffle...
                while (0 !== currentIndex) {

                    // Pick a remaining element...
                    randomIndex = Math.floor(Math.random() * currentIndex);
                    currentIndex -= 1;

                    // And swap it with the current element.
                    temporaryValue = array[currentIndex];
                    array[currentIndex] = array[randomIndex];
                    array[randomIndex] = temporaryValue;

                return array;

             * Returns a random integer between min (inclusive) and max (inclusive)
             * Using Math.round() will give you a non-uniform distribution!
            function getRandomInt(min, max) {
                return Math.floor(Math.random() * (max - min + 1)) + min;

🕑 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.
Loading ..................