Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- // Your JavaScript goes here!
- let playerScore = 0;
- let computerScore = 0;
- const PLAYER_WINS = 1;
- const COMP_WINS = 0;
- function computerPlay() {
- let computerTurn = Math.floor(Math.random() *3);
- switch (computerTurn) {
- case (0):
- return "rock";
- break;
- case (1):
- return "paper";
- break;
- case (2):
- return "scissors";
- break;
- }
- }
- function playRound(playerChoice, computerChoice) {
- if (
- (computerChoice == "paper" && playerChoice == "rock") ||
- (computerChoice == "scissors" && playerChoice == "paper") ||
- (computerChoice == "rock" && playerChoice == "scissors")
- ) {
- console.log("You lost the round");
- return COMP_WINS;
- } else if (
- (playerChoice == "paper" && computerChoice == "rock") ||
- (playerChoice == "scissors" && computerChoice == "paper") ||
- (playerChoice == "rock" && computerChoice == "scissors")
- ) {
- console.log("You won the round");
- return PLAYER_WINS;
- } else {
- console.log("It's a tie!")
- }
- }
- function game(n) {
- for (let i=0; i<n; i++) {
- let playerChoice = prompt("Rock, paper or scissors?: ".toLowerCase())
- let computerChoice = computerPlay();
- let resultRound = playRound(playerChoice, computerChoice);
- if (resultRound == COMP_WINS) {
- computerScore++;
- console.log("Computer: " + computerScore);
- console.log("Player: " + playerScore);
- } else if (resultRound == PLAYER_WINS) {
- playerScore++;
- console.log("Computer: " + computerScore);
- console.log("Player: " + playerScore);
- }
- }
- if (playerScore < computerScore) {
- console.log("The computer has won the game!")
- } else if (playerScore > computerScore) {
- console.log("The player has won the game!")
- } else {
- console.log("It is a tie!")
- }
- }
- let rounds = prompt("How many rounds would you like to play?: ")
- game(rounds)
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement