Issues with the counting of rounds in Rock Paper Scissors

This is my Javascript for a Rock Paper Scissors five-round game without using a loop. It is working great except for the “rounds” counting. I define it originally as 1 and in the if circuit of playRoud() it gets augmented in this fashion: round++, which I understand to be increase of 1 to the value of the variable. What is going on? How can I change this?

let round = 1;
let playerScore = 0;
let computerScore = 0; 

function computerPlay() {  
    let selectRandom = [Math.floor(Math.random() * 10) + 1]
    if (selectRandom <= 3) {
            return "rock"; }
    else if (selectRandom <= 6) {
            return "paper"; }
    else if (selectRandom >6) {
            return "scissors"; } 
}
       
function playRound(playerSelection, computerSelection) {
    if (playerSelection == computerSelection) {
        round++;
        return "It's a tie!"; }
    else if ((playerSelection == "rock") && (computerSelection == "paper")) {
        round++;
        computerScore++;
        return "You lose! Paper beats rock."; }
    else if ((playerSelection == "rock") && (computerSelection == "scissors")) {
        round++;
        playerScore++;
        return "You win! Rock beats scissors."; }
    else if ((playerSelection == "paper") && (computerSelection == "rock")) {
        round++;
        playerScore++;
        return "You win! Paper beats rock"; }
    else if ((playerSelection == "paper") && (computerSelection == "scissors")) {
        round++;
        computerScore++;
        return "You lose! Scissors beat paper.";  }
    else if ((playerSelection == "scissors") && (computerSelection == "rock")) {
        round++;
        computerScore++;
        return "You lose! Rock beats scissors"; }
    else if ((playerSelection == "scissors") && (computerSelection == "paper")) {
        round++;
        playerScore++;
        return "You win! Scissors beat paper."; }
}    

function bridgeRounds() {
    var select = prompt("Pick either rock, paper or scissors.");
    let playerSelection = select.toLowerCase();
    let computerSelection = computerPlay();
    
    console.log("Round: " + round);
    console.log("You have chosen " + playerSelection);
    console.log("Computer has chosen " + computerSelection);
    console.log(playRound(playerSelection, computerSelection));
    console.log("Your score: " + playerScore);
    console.log("Computer Score: " + computerScore); 
}
function game() {     

bridgeRounds();
computerPlay();
playRound();

bridgeRounds();
computerPlay();
playRound();

bridgeRounds();
computerPlay();
playRound();

bridgeRounds();
computerPlay();
playRound();

bridgeRounds();
computerPlay();
playRound();

if (playerScore > computerScore) {
    console.log("Final player Score: " + playerScore);
    console.log("Final computer Score: " + computerScore);
    console.log("You win the game!");}
else if (computerScore < playerScore) {
    console.log("Final player Score: " + playerScore);
    console.log("Final computer Score: " + computerScore);
    console.log("You lost!"); }

else if (computerScore == playerScore) {
    console.log("Final player Score: " + playerScore);
    console.log("Final computer Score: " + computerScore);
    console.log("It's a tie!"); }
}

game();

Perhaps you’re not viewing the actual current state of the variable since you’re running functions inside console.log().

From MDN: " Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object , which is not necessarily the ‘value’ of the object at the moment in time you call console.log() , but it is the value of the object at the moment you open the console."

Try to run functions outside of console.log(). You can very quickly test it by moving playRound(…) to its own line. Then see if it works, I am busy atm otherwise I’d give it a shot at home :smile:

I think I understand, though I won’t be sure until I put it to the test outside the console, as you suggest. Thank you.

1 Like

Hi,
You have round++ inside the playRound() function, but you call that function twice, once in bridgeRounds() and second time in the game() function, that’s why your round variable increases by 2 in each round. To fix that you can do like that:

 function game() {
        bridgeRounds();
       
        bridgeRounds();
        
        bridgeRounds();
       
        bridgeRounds();
       
        bridgeRounds();
      

        if (playerScore > computerScore) {
          console.log('Final player Score: ' + playerScore);
          console.log('Final computer Score: ' + computerScore);
          console.log('You win the game!');
        } else if (computerScore < playerScore) {
          console.log('Final player Score: ' + playerScore);
          console.log('Final computer Score: ' + computerScore);
          console.log('You lost!');
        } else if (computerScore == playerScore) {
          console.log('Final player Score: ' + playerScore);
          console.log('Final computer Score: ' + computerScore);
          console.log("It's a tie!");
        }
      }

You also don’t have to call computerPlay() inside the game() function as you are locking its returned value in the bridgeRounds() function:

let computerSelection = computerPlay();

I hope that makes sense to you, keep up the good work :slight_smile:

1 Like

Your response is very enlightening. I will do as you suggest. Thank you very much.