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

207
Views
Can't seem to figure out how to keep score in Rock, Paper, Scissors

I am trying to make Rock, Paper, Scissors via The Odin Project, and I'm stuck in the last step. I don't seem to have trouble getting the prompt to work, just can't seem to either tally up points, or I'm having trouble ending the game when either the player or computer gets to 5 points. I did include some console.logs at the bottom, but I'm unable to actually see the score due to the endless prompts. What am I doing wrong? I just need the game to end when either the player or computer gets to 5 points.

let playerScore = 0;
let computerScore = 0;

function computerPlay() {
    let allChoices = ["Rock", "Paper", "Scissors"];
    let randomChoice = allChoices[Math.floor(Math.random() * allChoices.length)];
    return randomChoice
}

function game() {
    while (playerScore <= 5 || computerScore <= 5) {
        const playerSelection = prompt("Would you like to choose R, P or S?")
        const computerSelection = computerPlay();
        alert(playRound(playerSelection, computerSelection));
    }
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection == computerSelection) {
        return "Tie game!"
    } else if (playerSelection == "Rock" && computerSelection == "Scissors") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else if (playerSelection == "Paper" && computerSelection == "Rock") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else if (playerSelection == "Scissors" && computerSelection == "Paper") {
        return `You win! ${playerSelection} beats ${computerSelection}!`
        playerScore += 1;
    } else {
        return `You lose! ${computerSelection} beats ${playerSelection}`
        computerScore += 1;
    }
}

function winGame() {
    if (playerScore == 5) {
        return "You win!"
    } else if (computerScore == 5) {
        return "You lose!"
    }
}

game();

console.log(playerScore);
console.log(computerScore);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function game() {
    while (playerScore < 5 && computerScore < 5) {
        const playerSelection = prompt("Would you like to choose Rock, Paper or Scissors?")
        const computerSelection = computerPlay();
        alert(playRound(playerSelection, computerSelection));
    }
}

function playRound(playerSelection, computerSelection) {
    if (playerSelection === computerSelection) {
        return "Tie game!"
    } else if (playerSelection === "Rock" && computerSelection === "Scissors") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`
        
    } else if (playerSelection === "Paper" && computerSelection === "Rock") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`
        
    } else if (playerSelection === "Scissors" && computerSelection === "Paper") {
        playerScore += 1;
        return `You win! ${playerSelection} beats ${computerSelection}!`

    } else {
        computerScore += 1;
        return `You lose! ${computerSelection} beats ${playerSelection}`
    }
}

You returned from the function before incrementing the points. So they will never be incremented. Also (playerScore <= 5 || computerScore <= 5) means, that the game will go on until both - playerScore and computerScore are below 6. It has to stop when one of them reaches 5

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!