Created
April 16, 2024 20:28
-
-
Save michaelcpuckett/96c421ab731f8b60b48c00f39833f13f to your computer and use it in GitHub Desktop.
Revisions
-
michaelcpuckett renamed this gist
Apr 16, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
michaelcpuckett created this gist
Apr 16, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ <button id="rock">Rock</button> <button id="paper">Paper</button> <button id="scissors">Scissors</button> <output> </output> <script> const buttonElements = Array.from(window.document.querySelectorAll("button")); const outputElement = window.document.querySelector("output"); function rps(playerChoice) { const winConditions = { rock: "scissors", paper: "rock", scissors: "paper", }; const computerChoice = Object.keys(winConditions)[Math.floor(Math.random() * 3)]; if (playerChoice === computerChoice) { return `Computer picked ${computerChoice}. It's a draw!`; } if (winConditions[playerChoice] === computerChoice) { return `Computer picked ${computerChoice}. You win!`; } return `Computer picked ${computerChoice}. Computer wins!`; } buttonElements.map((buttonElement) => { buttonElement.addEventListener("click", (event) => { const playerChoice = event.target.id; const result = rps(playerChoice); outputElement.textContent = result; }); }); </script>