javascriptfunction

Rock, Paper, Scissors game run for random number of rounds


I'm just learning JS and have built a Rock Paper Scissors game and am now trying to make it run for a fixed number of rounds (have not learned loop yet, so according to Odin Project course I'm trying to make it work without looping).

My problem is the game function. Right now, I've only copied the code inside the game function once, so it should only run twice in total. However, it seems to run for a random amount of times. Sometimes it'll prompt me 11 times before finishing the game, sometimes 2, sometimes 4. I notice in the console that not all inputs get logged but I don't see what I'm doing wrong.

const computerChoice = [
    "Rock",
    "Paper",
    "Scissors"
];

function getComputerChoice() {
    let random = Math.floor(Math.random()*computerChoice.length);
    let choice = computerChoice[random];
    return choice;
};

const computerSelection = getComputerChoice();

function playRound() {
    let lose = 'lose' 
    let win  = 'win'
    let tie  = 'tie'
    let wrong  = 'wrong'
    const playerSelection = getPlayerChoice();

    function getPlayerChoice() {
        let choice = prompt('Take your pick!');
        return choice;
    }

    if ((computerSelection === "Rock")&&(playerSelection === "Scissors") || (computerSelection === "Paper")&&(playerSelection === "Rock") || (computerSelection === "Scissors")&&(playerSelection === "Paper")) {
        return lose;
    }
    
    else if (computerSelection === playerSelection) {
        return tie;
    }
    
    else if ((playerSelection === "Rock")&&(computerSelection === "Scissors") || (playerSelection === "Paper")&&(computerSelection === "Rock") || (playerSelection === "Scissors")&&(computerSelection === "Paper")) {
        return win;
    }

    else {
        return wrong;
    }
}

let playerCounter = 0
let computerCounter = 0

function game(){
    if (playRound() === 'lose') {
        console.log('you lost this round');
        computerCounter++;
    }
    else if (playRound() === 'win') {
        console.log('you won this round');
        playerCounter++;
    }
    else if (playRound() === 'tie') {
        console.log("it's a tie");
    }
    else {
        console.log('wrong input');
    }
    if (playRound() === 'lose') {
        console.log('you lost this round');
        computerCounter++;
    }
    else if (playRound() === 'win') {
        console.log('you won this round');
        playerCounter++;
    }
    else if (playRound() === 'tie') {
        console.log("it's a tie");
    }
    else {
        console.log('wrong input');
    }

    if (computerCounter > playerCounter) {
        console.log('game over, pc won');
    }
    else if (playerCounter > computerCounter) {
        console.log ('game over, you won');
    }
    else {
        console.log ('game over, its a tie');
    }
}

game();

Solution

  • The issue you're facing with your code is that the playRound() function is called multiple times within the game() function, and each time it's called, it generates a new random choice for the computer. This is why you're getting a random number of prompts and results.

    You can see in below fixed code, optimised some for you:

        const computerChoice = [
        "Rock",
        "Paper",
        "Scissors"
    ];
    
    function getComputerChoice() {
        let random = Math.floor(Math.random() * computerChoice.length);
        let choice = computerChoice[random];
        return choice;
    }
    
    let playerCounter = 0;
    let computerCounter = 0;
    
    function playRound(playerSelection, computerSelection) {
        if (
            (computerSelection === "Rock" && playerSelection === "Scissors") ||
            (computerSelection === "Paper" && playerSelection === "Rock") ||
            (computerSelection === "Scissors" && playerSelection === "Paper")
        ) {
            return 'lose';
        } else if (computerSelection === playerSelection) {
            return 'tie';
        } else {
            return 'win';
        }
    }
    
    function game() {
        const playerSelection = getPlayerChoice();
        const computerSelection = getComputerChoice();
    
        console.log(`You chose: ${playerSelection}`);
        console.log(`Computer chose: ${computerSelection}`);
    
        const result = playRound(playerSelection, computerSelection);
    
        if (result === 'lose') {
            console.log('You lost this round');
            computerCounter++;
        } else if (result === 'win') {
            console.log('You won this round');
            playerCounter++;
        } else {
            console.log("It's a tie");
        }
    
        if (computerCounter > playerCounter) {
            console.log('Game over, PC won');
        } else if (playerCounter > computerCounter) {
            console.log('Game over, you won');
        } else {
            console.log('Game over, it\'s a tie');
        }
    }
    
    function getPlayerChoice() {
        let choice = prompt('Take your pick! (Rock, Paper, or Scissors)').toLowerCase();
        
        // Check if the player's choice is valid
        if (choice === 'rock' || choice === 'paper' || choice === 'scissors') {
            return choice.charAt(0).toUpperCase() + choice.slice(1);
        } else {
            console.log('Wrong input. Please choose Rock, Paper, or Scissors.');
            return getPlayerChoice(); // Recursively ask for input until it's valid
        }
    }
    
    game();