Created
September 9, 2012 18:40
-
-
Save benjaffe2/3686352 to your computer and use it in GitHub Desktop.
JS: When a secret code is typed, do something!
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
var currentPos = 0; | |
var code = "angel"; | |
var codeLength = code.length; | |
var proceed = false; | |
var lastKeysPressed = new Array(codeLength); | |
$(document).keyup(function(e) { | |
lastKeysPressed[currentPos%codeLength] = String.fromCharCode(e.keyCode).toLowerCase(); | |
currentPos++; | |
if (checkCode()) { //last check | |
proceed = confirm("Congratulations! You typed the code.\n(The coooo-o-o-ooo-ooo-ooo-o-o-ooo...ooo-o-o-ooo-ooo-oooooode)\n\nDo you wish to proceed into a land with laws dictated only by the sometimes scary but always unique ramblings of Emilys' minds?\n\nThis is your last chance to turn back.\n\n"); | |
} | |
if (proceed) { | |
proceed = false; | |
alert("Credit for the following page goes to Emily Block"); | |
$.mobile.showPageLoadingMsg(); | |
document.location.href = 'eb/home.html'; | |
} | |
function checkCode () { | |
for (var i=0; i < lastKeysPressed.length; i++) { //run through each key pressed | |
for (var j=0; j < lastKeysPressed.length; j++) { //try to line up the first letter | |
if (lastKeysPressed[j] === code[(j+i)%codeLength]) { //if this is true, at least the first letter is aligned (j+i) | |
for (var k=0, doesThisPass=true; k < lastKeysPressed.length; k++) { //now that we're aligned, run through each letter and check it | |
if (lastKeysPressed[k%codeLength] !== code[(k+i)%codeLength]) { //if it doesn't match, abort | |
doesThisPass = false; | |
continue; | |
} | |
} | |
if (doesThisPass) { //if we didn't fail any tests, the code was typed successfully. | |
return true; | |
} | |
} | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment