Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assistance in implementing the modal aspect. Take a look at the code below for more information:
var pattern = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a'];
var current = 0;
var keyHandler = function (event) {
// If the pressed key is not in the pattern or does not match the current key, reset
if (pattern.indexOf(event.key) < 0 || event.key !== pattern[current]) {
current = 0;
return;
}
// Update the progress of typing the pattern
current++;
// If the entire pattern is successfully entered, trigger alert and reset
if (pattern.length === current) {
current = 0;
// This is where the Modal feature should be implemented with options for dark mode and light mode.
}
};
// Listen for keydown events
document.addEventListener('keydown', keyHandler, false);
<h1> Enter the Konami code. (↑↑↓↓←→←→BA) to uncover a surprise.