steveosoule
3/25/2017 - 8:38 PM

Konami Code using jQuery & Lodash

Konami Code using jQuery & Lodash

(function konami(){

		var keys = {
			38: 'up',
			40: 'down',
			37: 'left',
			39: 'right',
			66: 'b',
			65: 'a'
		};

		var konami_code = ['up','up','down','down','left','right','left','right','b','a'];
		var keydown_series = [];

		var onKeyDown = function(e){
			if( !keys.hasOwnProperty(e.which) ){
				keydown_series = [];
				return;
			}

			keydown_series.push(keys[e.which]);
			// console.log(keydown_series);

			if( !_.isMatch(konami_code, keydown_series) ){
				keydown_series = [];
				return;
			}

			if( _.isEqual(konami_code, keydown_series) ){
				keydown_series = [];
				unlocked();
			}
		};
		
		var unlocked = function(){
			console.log('Cheers! ');
		};

		$(window).on('keydown', onKeyDown);
	})();