Skip to content

Commit

Permalink
End the game on "a" or "l" only
Browse files Browse the repository at this point in the history
Based on the description on MDN, pressing a wrong key shouldn't end the game, right?

> Regardless of which one of the player control keys was pressed, remove the `keydown` event listener using `removeEventListener()` so that once the winning press has happened, no more keyboard input is possible to mess up the final game result.
  • Loading branch information
mrmowji authored Jun 19, 2020
1 parent 01ca141 commit 3bb5063
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions javascript/asynchronous/loops-and-intervals/reaction-game.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,21 @@
document.addEventListener('keydown', keyHandler);

function keyHandler(e) {
let isOver = false;
console.log(e.key);
if(e.key === "a") {

if (e.key === "a") {
result.textContent = 'Player 1 won!!';
} else if(e.key === "l") {
isOver = true;
} else if (e.key === "l") {
result.textContent = 'Player 2 won!!';
isOver = true;
}

document.removeEventListener('keydown', keyHandler);
setTimeout(reset, 5000);
if (isOver) {
document.removeEventListener('keydown', keyHandler);
setTimeout(reset, 5000);
}
};
}
</script>
Expand Down

0 comments on commit 3bb5063

Please sign in to comment.