Konami Code

In this example below you will see how to do a Konami Code with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by jmm, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright jmm ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Konami Code</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/jmm/konami-code-jYMXqo */
const secretCode = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a', 'Enter', ]
let pressedKeys = [];

window.addEventListener('keyup', (e) => {
  pressedKeys.push(e.key);
  const matchSoFar = pressedKeys.reduce((previous, value, i) => previous && value === secretCode[i], true);

  if (!matchSoFar) {
    pressedKeys = [];
  } else if (pressedKeys.length === secretCode.length) {
    document.body.innerHTML = '<h1>Winner winner, chicken dinner!</h1>';
    pressedKeys = [];
  }
})

Comments