Skip to content

Instantly share code, notes, and snippets.

@michaelcpuckett
Created April 16, 2024 20:28
Show Gist options
  • Save michaelcpuckett/96c421ab731f8b60b48c00f39833f13f to your computer and use it in GitHub Desktop.
Save michaelcpuckett/96c421ab731f8b60b48c00f39833f13f to your computer and use it in GitHub Desktop.

Revisions

  1. michaelcpuckett renamed this gist Apr 16, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. michaelcpuckett created this gist Apr 16, 2024.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original 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>