Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Making "Rounds" in a Rock-Paper-Scissors Game?

I know this is a very popular/simple game to make, but I'm having a bit of trouble. I have made a rock-paper-scissors game in javascript and I've managed to make it work so that it will prompt the player to choose rock, paper, or scissors, get the computer to randomly pick an option, and a means for it to say "you lose" or "you win". The problem is that I'm required to make 5 rounds. That's the issue I'm having. I, for some reason, cannot seem to get the code to prompt the user 5 times. It only prompts the user once and runs the same code each time. My code is as follows:

     function playRound(playerSelection) {
    let computerSelection = getComputerSelection();
    if (playerSelection == "rock") {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "It's a tie";
        }
        else if (computerSelection == "paper") {
            result = "You lost! Paper beats Rock";
            computerScore++;
        }
        else {
            result = "You won! Rock beats Scissors";
            playerScore++;
        }
    }
    else if (playerSelection == "paper") {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "You won! Paper beats Rock";
            playerScore++;
        }
        else if (computerSelection == "paper") {
            result = "It's a tie";
        }
        else {
            result = "You lost! Scissors beats Paper";
            computerScore++;
        }
    }
    else {
        addToList(playerSelection, computerSelection);
        if (computerSelection == "rock") {
            result = "You lost! Rock beats Scissors";
            computerScore++;
        }
        else if (computerSelection == "paper") {
            result = "You won! Scissors beats Rock";
            playerScore++;
        }
        else
        result = "It's a tie";
    }
    h1.textContent = result;
    player.textContent = playerScore;
    computer.textContent = computerScore;
    game();
}

function game() {
    if (playerScore === 5) {
        end.style.display = "block";
        message.textContent = "You Won!";
    }
    else if (computerScore === 5) {
        end.style.display = "block";
        message.textContent = "You Lost!";
    }
}

I would appreciate any help or direction that is given. I have tried putting my "playRound" function outside of the loop, but it gives me the same issue. What would make it prompt twice?

Thank you!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I'm no expert in JS, so please correct me if i'm wrong, but what seems to be the case is that this variable:

const playerSelection = window.prompt("Enter your choice: ").toLowerCase();

is only initialized a single time with that function's return. (because it is outside any function) Accessing the variable only recalls it's value it was initialized with, causing the main loop to run over and over again with only that initial value.

replacing that line with someing like:

function getPlayerSelection(){
    return window.prompt("Enter your choice:").toLowerCase()
}

and replacing everywhere you use playerSelection with getPlayerSelection() should fix this issue.

about 4 years ago · Juan Pablo Isaza Report

0

That's because you are asking for player's prompt only once i.e. it is outside the playRound method. Try moving the following snippet into playround method.

const computerSelection = computerPlay();

const playerSelection = window.prompt("Enter your choice: ").toLowerCase();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!