Skip to content

Instantly share code, notes, and snippets.

@zgulde
Last active February 24, 2016 03:25
Show Gist options
  • Select an option

  • Save zgulde/0a7a9f21d53486207f63 to your computer and use it in GitHub Desktop.

Select an option

Save zgulde/0a7a9f21d53486207f63 to your computer and use it in GitHub Desktop.
a functional programming approach to listening for the konami code
// 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