Replace it with the obvious 10-state Knuth–Morris–Pratt FSM.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
-var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
+var konami = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
+ konami_jump = [-1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
+ konami_state = 0;
document.onkeydown = function (e) {
document.onkeydown = function (e) {
- kkeys.push(e.keyCode);
- var idx = kkeys.toString().indexOf( konami );
- if (idx >= 0 && idx != kkeys.toString().length - konami.length) {
- kkeys = [];
- window.location.assign("http://nyan.cat/");
+ while (konami_state != -1 && e.keyCode != konami[konami_state]) {
+ konami_state = konami_jump[konami_state];
+ }
+ if (++konami_state == konami.length) {
+ window.location.assign("http://nyan.cat/");
+ konami_state = 0;