I was asked to implement the Konami Code in a website I\'m currently working on. It should do the following:
Change Background Image
Play s
My own compact and cleaned version inspired by the answers here:
let cursor = 0;
const KONAMI_CODE = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
document.addEventListener('keydown', (e) => {
cursor = (e.keyCode == KONAMI_CODE[cursor]) ? cursor + 1 : 0;
if (cursor == KONAMI_CODE.length) activate();
});
In this case, the activate()
function is called when triggered.