Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
11
Indexable
Never
let playerScore, computerScore, playerSelection, computerSelection, roundWinner;
let resultadoFinal=[]; 

function getComputerChoice(){
    const eleccionrandom = ["rock","paper","scissors"];
    const numerorandom = Math.floor(Math.random()*3);
    return eleccionrandom[numerorandom];

}
function oneRound(playerSelection, computerSelection){
    
    if (
        (playerSelection === "rock" && computerSelection === "scissors") || 
        (playerSelection === "paper" && computerSelection === "rock") || (playerSelection === "scissors" && computerSelection === "paper")){
        playerScore++;
        roundWinner = "player" 
    }else if (
            (computerSelection === "rock" && playerSelection === "scissors") || 
            (computerSelection === "paper" && playerSelection === "rock") || (computerSelection === "scissors" && playerSelection === "paper")){
            computerScore++;
            roundWinner = "computer" 
                
    }else if (playerSelection === computerSelection){
        roundWinner="Empate";
    }else {
        roundWinner="error";
    } return roundWinner;
}

function game(){
    for (let i = 0; i < 5; i++){
        computerSelection = getComputerChoice();
        playerSelection = prompt("rock, paper or scissors?");
        oneRound(playerSelection, computerSelection); 
        resultadoFinal[i]= oneRound();  
    }   console.log("el resultado es " + resultadoFinal);
}
game();