Skip to content

Instantly share code, notes, and snippets.

@kissu
Created February 14, 2025 17:33
Show Gist options
  • Save kissu/1fbf36f4e072480d6e631f50bd0fbc89 to your computer and use it in GitHub Desktop.
Save kissu/1fbf36f4e072480d6e631f50bd0fbc89 to your computer and use it in GitHub Desktop.
Wagon Race
// TODO: write your code here
const moveForward = (player) => {
const wagon = document.querySelector(`#player${player}-race .active`);
if (wagon.nextElementSibling) {
wagon.nextElementSibling.classList.add('active');
wagon.classList.remove('active');
} else {
alert(`Player ${player} wins! Play again?`);
window.location.reload();
}
};
const moveWagons = (event) => {
if (event.key === "q") {
moveForward(1);
} else if (event.key === "p") {
moveForward(2);
}
};
// NOTE: Listening to "keyup" prevents multiple events being fired by a single long press of the key
// 👉 https://unixpapa.com/js/testkey.html
document.addEventListener("keyup", moveWagons);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment