Created
May 21, 2019 18:08
-
-
Save zanbaldwin/14f4012738b3890ef513f3b6d64bb1c9 to your computer and use it in GitHub Desktop.
Konami Code in Javascript/TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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