Last active
February 24, 2016 03:25
-
-
Save zgulde/0a7a9f21d53486207f63 to your computer and use it in GitHub Desktop.
a functional programming approach to listening for the konami code
This file contains hidden or 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
| // include jquery | |
| var konamiCode = [38,38,40,40,37,39,37,39,66,65,13]; | |
| function rest(array){ | |
| return array.slice(1, array.length); | |
| } | |
| function first(array){ | |
| return array[0]; | |
| } | |
| function onCodeEntered(){ | |
| alert('functiontastic!'); | |
| } | |
| function checkCode(currentSequence){ | |
| if (currentSequence.length){ | |
| return function(e){ | |
| $(document).off('keyup'); | |
| $(document).on('keyup', | |
| checkCode((first(currentSequence) == e.which) ? rest(currentSequence) : konamiCode)); | |
| }; | |
| } else { | |
| onCodeEntered(); | |
| } | |
| } | |
| $(document).on('keyup', checkCode(konamiCode)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment