GithubHelp home page GithubHelp logo

ordoflammae / konami-code-lab-bootcamp-prep-000 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/konami-code-lab-bootcamp-prep-000

0.0 1.0 0.0 45 KB

License: Other

HTML 46.49% JavaScript 53.51%

konami-code-lab-bootcamp-prep-000's Introduction

Konami Code Lab

Objectives

  1. Practice using event listeners.
  2. Explain how event listeners are triggered.
  3. Capture user interactions to trigger events on a page.

Instructions

In konami_code.js, you'll notice that we've provided very little: well, nothing except -- what's that? If you open the file up in your text editor, you should see:

const codes = [
  "ArrowUp",
  "ArrowUp",
  "ArrowDown",
  "ArrowDown",
  "ArrowLeft",
  "ArrowRight",
  "ArrowLeft",
  "ArrowRight",
  "b",
  "a"
];

function init() {
  // your code here
}

But what could those keycodes be? They're the famous Konami Code, as JavaScript KeyboardEvent keys. It's become a common Easter egg for sites to have hidden features behind this code, and now it's your turn to implement it!

In index.html, you'll see that we're loading the file in for you:

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

This is JavaScript's way of pulling in code from outside the page. Here, we've given the <script> tag a local source (the file that's right here in the directory), but we could also supply a URL to load an external resource (more on that in a bit).

You'll want to attach an event listener to document.body and check for 'keydown' events. If the user enters this special code, pressing all ten of the keys in the correct order, alert() a congratulatory message. However, if they press a key out of sequence or a key that isn't part of the Konami code, don't alert() anything and simply keep listening for all ten keydowns in the correct order.

When you're testing the code out in the browser, be sure to call init() to attach the event listener and set everything up!

Stuck on how to get started? Here's a contrived, short example:

// Keys for A, B, and C keys.
const alphabet = ['a', 'b', 'c'];

// Keep track of index outside of the event handler.
let index = 0;

// This is the function that would be invoked by the event listener.
function onKeyDownHandler(e) {
  const key = e.key;

  if (key === alphabet[index]) {
    index++;

    if (index === alphabet.length) {
      alert("Hurray!");

      index = 0;
    }
  } else {
    index = 0;
  }
}

Have fun!

Hints

KeyboardEvent has gotten lots of recent updates. The key and code properties recently replaced which, keyCode, and charCode properties, which were often implemented slightly differently between different browsers and would report different values across different operating systems. Some environments (node in particular) don't know about KeyboardEvent

Resources

View Konami Code Lab on Learn.co and start learning to code for free.

konami-code-lab-bootcamp-prep-000's People

Contributors

danielseehausen avatar gj avatar ines66 avatar josh-stillman avatar pletcher avatar rachelsa avatar rrcobb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.