Skip to content

Instantly share code, notes, and snippets.

@zanbaldwin
Created May 21, 2019 18:08
Show Gist options
  • Save zanbaldwin/14f4012738b3890ef513f3b6d64bb1c9 to your computer and use it in GitHub Desktop.
Save zanbaldwin/14f4012738b3890ef513f3b6d64bb1c9 to your computer and use it in GitHub Desktop.
Konami Code in Javascript/TypeScript
function konami(callback) {
var codes = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
position = 0;
document.addEventListener('keydown', function (event) {
if (event.keyCode === codes[position]) {
position++;
if (position === codes.length) {
position = 0;
callback();
}
} else {
position = 0;
}
});
}
function konami(callback: () => void): void {
let codes: number[] = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
position: number = 0;
document.addEventListener('keydown', function (event: KeyboardEvent): void {
if (event.keyCode === codes[position]) {
position++;
if (position === codes.length) {
position = 0;
callback();
}
} else {
position = 0;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment