Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

According to the DRY-principle, any ideas how to write this a bit shorter?

jQuery.konami = function(fn, code) {
    // ↑ ↑ ↓ ↓ ← → ← → B A
    code = code || [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];

    var kkeys = '',
        i = 0;

    $(document).keydown(function(e) {
        var char = String.fromCharCode(e.which).toLowerCase();
        if (char === code[i++]) {
            kkeys += char;

            if (kkeys === code) {
                fn();
                kkeys = '';
                i = 0;
            }
        } else if (e.which === code[kkeys++]) {
            if (kkeys === code.length) {
                fn();
                kkeys = '';
                i = 0;
            }
        } else {
            kkeys = '';
            i = 0;
        }
    });
};

Update

Thanks to NickLarsen for his help. Here's the final code.

share|improve this question

migrated from stackoverflow.com Dec 3 '12 at 15:33

This question came from our site for professional and enthusiast programmers.

    
@KevinB nice did not know that this site exist, thanks! –  yckart Nov 30 '12 at 15:17
add comment

2 Answers 2

This is almost like code golf; this could probably be shortened. You just need to keep track of i and when it's past the end of the array, you know all the keys were hit in the correct order.

jQuery.konami = function() {
    function KonamiCode(kFn, kCode) {
        var i = 0;

        $(document).keydown(function(e) {
            var char = typeof kCode === 'string' ? String.fromCharCode(e.which).toLowerCase() : e.which;
            i = char === kCode[i] ? i + 1 : 0;
            if (i === kCode.length) {
                kFn();
                i = 0;
            }
        });
    }
    return function(fn, code) {
        // ↑ ↑ ↓ ↓ ← → ← → B A
        kCode = code || [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
        new KonamiCode(fn, kCode);
    };
}();
share|improve this answer
    
nice approach, but works not as expected... –  yckart Nov 30 '12 at 15:19
    
@yckart what's wrong with it? –  NickLarsen Nov 30 '12 at 15:20
    
yeah it just simply isn't working, :p jsfiddle.net/dxPdw/5 I haven't investigated as to why though –  Kevin B Nov 30 '12 at 15:22
    
@NickLarsen it breaks on multiple instantiations and even fails with strings (words)... jsfiddle.net/ARTsinn/dxPdw/6 –  yckart Nov 30 '12 at 15:22
    
Silly logic error, i = char === code[i] ? 0 : i + 1; needed to be i = char === code[i] ? i + 1 : 0;. –  NickLarsen Nov 30 '12 at 15:25
show 13 more comments

Here's a slightly shortened version:

jQuery.konami = function(fn, code) {
    // ↑ ↑ ↓ ↓ ← → ← → B A
    code = code || [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];

    var i = 0;

    $(document).keydown(function(e) {
        var char = $.type(code[i]) === "string" ? String.fromCharCode(e.which).toLowerCase() : e.which;
        if (char === code[i]) {
            i++;
            if (i == code.length) {
                fn();
                i = 0;
            }
        } else {
            i = 0;
        }
    });
};

the kkeys var wasn't needed, and you could move the if else into the retrieval of char, allowing you to reuse what was inside the if originally.

http://jsfiddle.net/dxPdw/12/

share|improve this answer
    
Nice! However, got it already stackoverflow.com/questions/13647824/… ;-) –  yckart Nov 30 '12 at 15:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.