# Konami Code in JavaScript

**URL:** https://forum.kirupa.com/t/konami-code-in-javascript/640851
**Category:** web dev
**Created:** [October 15, 2019, 5:21am UTC](https://forum.kirupa.com/t/konami-code-in-javascript/640851 "2019-10-15T05:21:22Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [September 30, 2022, 11:33pm UTC](https://forum.kirupa.com/t/konami-code-in-javascript/640851/2 "2022-09-30T23:33:18Z")

</div>

This is more of a matching game where you have to match the sequence to clear lines, each line being a collection of the keys you’ve used between hitting ENTER. But you start off with the first line being the Konami code, so it applies 😉

```javascript
const replacements = {
	"ArrowUp": "⬆️",
	"ArrowDown": "⬇️",
	"ArrowLeft": "⬅️",
	"ArrowRight": "➡️",
};

const root = document.body.appendChild(document.createElement("div"));

function newLine() {
  return root.appendChild(document.createElement("p"));
}

function addKey(key) {
  root.lastElementChild.innerHTML += `<button>${replacements[key] ?? key}</button>`;
}

function clearMatches() {
  const curr = root.lastElementChild;
  const prev = curr.previousElementSibling;
  if (prev?.innerHTML === curr.innerHTML) {
    newLine();
    setTimeout(() => (curr.remove(), prev.remove()), 500);
  }
}

window.onkeyup = ({ key }) => {
  if (key === "Enter") {
    if (root.lastElementChild.childElementCount) newLine();
    return;
  }
  addKey(key);
  clearMatches();
}

newLine();
addKey("ArrowUp");
addKey("ArrowUp");
addKey("ArrowDown");
addKey("ArrowDown");
addKey("ArrowLeft");
addKey("ArrowRight");
addKey("ArrowLeft");
addKey("ArrowRight");
addKey("b");
addKey("a");
newLine();

```

P.S. You win nothing by clearing all the lines 🙃

[JSFiddle link](https://jsfiddle.net/xvLphtaq/1/)

---

_[View the full topic](https://forum.kirupa.com/t/konami-code-in-javascript/640851)._
