Giter Site home page Giter Site logo

konami-code-lab-v-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, as shown in the example above. 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.

function init() {

  // Attaching an keydown event listener to document.body
  document.body.addEventListener("keydown", (event) => {
    // Now, how can we check for which specific key was pressed?
  }
}

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!

Note: Once you have the tests passing you still may not see the alert pop up if you try to test your code in the browser. This is because the test 'intercepts' the alert. To see it work in the browser, temporarily comment out all the code in the indexTest.js file except the line where init() is called.

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

If you are working on this lab using the in-browser IDE, you can see your progress in browser by using httpserver and navigating to the provided IP. You will need to close out of httpserver and run learn to test your solution.

Resources

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

konami-code-lab-v-000's People

Contributors

danielseehausen avatar drakeltheryuujin avatar gj avatar ines66 avatar josh-stillman avatar lizbur10 avatar maxwellbenton avatar pletcher avatar rachelsa avatar rrcobb avatar sammarcus avatar taoliu12 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

konami-code-lab-v-000's Issues

Error in solution (and example provided in curriculum)

The second-to-last element in the code (66) triggers the alert instead of the last element (65).

This is due toindex being incremented to 9 after the condition if (66 === code[8])... is met, which is immediately followed by the conditional if (9 === code.length -1)... that is also met.

The solution only needs to change slightly. Instead of if (index === code.length - 1), it should be if (index === code.length).

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.