cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              .frame#intro.jumbotron.jumbotron-fluid
  .container
    iframe(width="854" height="480" src="https://www.youtube.com/embed/zsXP8qeFF6A" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen)

    hr.my-4

    .row
      .col-sm-6
        p.lead
          | Check out this video
          br
          | Are you ready to take the test ?
      .col-sm-6
        p.lead
          | TL;DR
          br
          | Memorize and press buttons in ascending order
    button#start.btn.btn-primary.btn-lg Challenge accepted.

.frame#game
  each i in [1,2,3,4,5,6,7,8,9]
    button(disabled).btn.number= i

.frame#results.jumbotron.jumbotron-fluid
  .container
    h1.display-4 You... 
      span#result should not be looking at this
    
    hr.my-4
    
    p.lead Chimps average: 90%
    p.lead Your average: 
      span#avg
      | %
    
    hr.my-4
    
    button#replay.btn.btn-primary.btn-lg Let me have another try
    
    hr.my-4
    
    p
      | Made with 💗 by 
      a(href='https://codepen.io/ninivert/') ninivert
            
          
!
            
              body,
html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: auto;
}

.frame {
  min-height: 100vh;
  margin: 0;
  // overflow: auto;
}

#intro {
  iframe {
    max-width: 100%;
  }
}

#game {
  position: relative;
  background: #212529;
  overflow: hidden;
  
  .number {
    // Styling
    padding: 0;
    line-height: 100%;
    font-size: 2em;
    transition: top .5s cubic-bezier(0.68, -0.55, 0.265, 1.55),
                left .5s cubic-bezier(0.68, -0.55, 0.265, 1.55),
                opacity .1s, visibility .1s;
    // Positionning is set by JS
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    // Hide animation
    visibility: visible;
    opacity: 1;
    &.hide {
      visibility: hidden;
      opacity: 0;
    }
  }
}

            
          
!
            
              /**
 * Dom Declaration
 * Global option variables declaration
 */
const dom = {
  intro: document.getElementById('intro'),
  game: document.getElementById('game'),
  results: document.getElementById('results'),
  numbers: document.getElementsByClassName('number'),
  start: document.getElementById('start'),
  replay: document.getElementById('replay'),
  result: document.getElementById('result'),
  avg: document.getElementById('avg')
};
let w, h, opts = {}, game = {};



/**
 * Global setup function
 */
function setup() {
  w = window.innerWidth;
  h = window.innerHeight;
  opts.grid = Math.floor(Math.min(w, h)/8);
  opts.margin = 20;
  game.gamesWon = 0;
  game.gamesPlayed = 0;
  
  for (let i=0; i<dom.numbers.length; i++) {
    dom.numbers[i].style.width = opts.grid + 'px';
    dom.numbers[i].style.height = opts.grid + 'px';
    // Need to set number attribute because I can't rely on innerHTML
    dom.numbers[i].setAttribute('number', i+1);
    dom.numbers[i].onclick = function() { step(this); };
  }
}



/**
 * Reset
 */
function reset() {
  // Game variables
  game.currentNumber = 0;
  ++game.gamesPlayed;
  
  // Show the game
  dom.game.scrollIntoView({ 
    behavior: 'smooth'
  });
  
  // Lock buttons during animation and show them and add numbers
  for (let i=0; i<dom.numbers.length; i++) {
    dom.numbers[i].disabled = true;
    dom.numbers[i].classList.remove('hide');
    dom.numbers[i].innerHTML = i+1;
  }
  
  // Game setup animation
  setTimeout(function() {
    // Button animation
    let spots = [];

    // Generate possible spots
    for (let x=opts.grid/2+opts.margin*2; x<w-opts.grid/2-opts.margin*2; x+=opts.grid+opts.margin) {
      for (let y=opts.grid/2+opts.margin*2; y<h-opts.grid/2-opts.margin*2; y+=opts.grid+opts.margin) {
        spots.push({x: x, y: y});
      }
    }

    let pick, spot;

    // Pick a random spot for each number
    for (let i=0; i<dom.numbers.length; i++) {
      pick = Math.floor(Math.random()*spots.length);
      spot = spots.splice(pick, 1)[0];
      dom.numbers[i].style.left = spot.x + 'px';
      dom.numbers[i].style.top = spot.y + 'px';
    }

    setTimeout(function () {
      // Unlock buttons
      for (let i=0; i<dom.numbers.length; i++) {
        dom.numbers[i].disabled = false;
      }
    }, 500);
  }, 600);
}



/**
 * Game step
 * Called on each number press
 */
function step(numberBtn) {
  // Get user number input from the button
  let userNumber = parseInt(numberBtn.getAttribute('number'));
  
  // Hide everything on the first click
  if (game.currentNumber === 0) {
    for (let i=0; i<dom.numbers.length; i++) {
      dom.numbers[i].innerHTML = '';
    }
  }
  
  // Increment tested number
  // And check if input is correct
  if (++game.currentNumber === userNumber) {
    numberBtn.classList.add('hide');
    
    // Game is won
    if (game.currentNumber === dom.numbers.length) {
      dom.result.innerHTML = 'won!';
      ++game.gamesWon;
      end();
    }
  }
  
  // Game is lost
  else {
    // Show the user where they messed up
    numberBtn.innerHTML = numberBtn.getAttribute('number');
    
    dom.result.innerHTML = 'lost.';
    setTimeout(end, 500);
  }
  
  // End the game
  function end() {
    // Lock buttons during animation and show them and add numbers
    for (let i=0; i<dom.numbers.length; i++) {
      dom.numbers[i].disabled = true;
    }
    // Update the average and score
    dom.avg.innerHTML = Math.round(game.gamesWon/game.gamesPlayed*100);
    dom.results.scrollIntoView({
      behavior: 'smooth'
    });
  }
}



/**
 * Controls
 */
dom.start.addEventListener('click', reset);
dom.replay.addEventListener('click', reset);
window.addEventListener('resize', setup);
setup();
            
          
!
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.
Loading ..................

Console