Untitled
unknown
plain_text
a year ago
2.8 kB
3
Indexable
// Initialize the score of the computer and the human globally let ComputerScore = 0; let HumanScore = 0; let TieCounter = 0; function getRandomInt(max) { return Math.floor(Math.random() * max); } function getComputerChoice() { // Use getRandomInt to make the computer randomly choose. const choices = ["rock", "paper", "scissors"]; return choices[getRandomInt(3)]; } // Create a function that determines the winner. function CheckWinner(ComputerChoice, HumanChoice) { if (ComputerChoice === HumanChoice) { return "Tie"; } else { if (ComputerChoice === "rock") { if (HumanChoice === "paper") { return "Human"; } else if (HumanChoice === "scissors") { return "Computer"; } } else if (ComputerChoice === "paper") { if (HumanChoice === "rock") { return "Computer"; } else if (HumanChoice === "scissors") { return "Human"; } } else if (ComputerChoice === "scissors") { if (HumanChoice === "rock") { return "Human"; } else if (HumanChoice === "paper") { return "Computer"; } } } } function playRound(HumanChoice) { const ComputerChoice = getComputerChoice(); const winner = CheckWinner(ComputerChoice, HumanChoice); console.log(`Computer chose: ${ComputerChoice}`); console.log(`Human chose: ${HumanChoice}`); console.log(`Winner: ${winner}`); if (winner === "Computer") { ComputerScore++; } else if (winner === "Human") { HumanScore++; } else { TieCounter++; } // Update scores after each round console.log(`Scores - Computer: ${ComputerScore}, Human: ${HumanScore}, Tie: ${TieCounter}`); } function updateGameResult(result) { const gameResultElement = document.getElementById('gameresult'); gameResultElement.textContent = result; } const rock = document.querySelector('#rock'); const paper = document.querySelector('#paper'); const scissors = document.querySelector('#scissors'); let HumanChoice; // Declare HumanChoice as a global variable rock.addEventListener('click', function() { HumanChoice = 'rock'; console.log("Rock clicked.") }); paper.addEventListener('click', function() { HumanChoice = 'paper'; console.log("Paper clicked.") }); scissors.addEventListener('click', function() { HumanChoice = 'scissors'; console.log("Scissors clicked.") }); function playGame() { let rounds = 0; if (rounds >= 5) { playRound(HumanChoice); rounds += 1; } } // Define a function to get the human's choice. // Call playGame after setting up event listeners playGame();
Editor is loading...
Leave a Comment