Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
1
Indexable
Never
let playerScore=0;
let computerScore=0;
let roundWinner = ''

function computerPlay() {
    const fighter = ["Rock", "Paper", "Scissors"];
    const computerSelection = fighter[Math.floor(Math.random() * fighter.length)];
    return computerSelection;
}

function playRound(playerSelection, computerSelection) {
    playerSelection = playerSelection.toLowerCase();
    computerSelection = computerPlay().toLowerCase();

    if (playerSelection === computerSelection) {
        console.log ("It's a tie!")
    } 
    else if (
        (playerSelection === "paper" && computerSelection === "rock") ||
        (playerSelection === "rock" && computerSelection === "scissors") ||
        (playerSelection === "scissors" && computerSelection === "paper")
    ) {
        playerScore++
        console.log ("You won " + playerSelection + " beats " + computerSelection + "!")
        
    }
    else if (
        (computerSelection === "paper" && playerSelection === "rock") ||
        (computerSelection === "rock" && playerSelection === "scissors") ||
        (computerSelection === "scissors" && playerSelection === "paper")
    ) {
        computerScore++
        console.log ("You lost " + computerSelection + " beats " + playerSelection + "!")
    }    
}


function game() {
    playRound(playerSelection, computerSelection) 
        console.log ("Player score is " + playerScore + ", Computer score is " + computerScore)
    }


for (let i=0; i<5; i++) {
    const playerSelection = "rock";
    const computerSelection = computerPlay();
    console.log(game())
}