Created
February 14, 2025 17:33
-
-
Save kissu/1fbf36f4e072480d6e631f50bd0fbc89 to your computer and use it in GitHub Desktop.
Wagon Race
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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