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

128
Views
Rock Paper Scissors game only one outcome unless I refresh page

So im making The Odin Project's rock paper scissors game and the only problem i have is that the computer's selection is always the same unless I refresh the page.

for example if i choose Rock three times in a row, the computer's selection will always be the same unless i refresh the page.

const thing1 = "Rock",
    thing2 = "Paper",
    thing3 = "Scissors";

function computerPlay() {
    let a = Math.random();
    if (a <= 0.33) {
        return thing1;
    } else if (a > 0.33 && a < 0.67) {
        return thing2;
    } else {
        return thing3;
    }
}

let playerScore = 0,
    computerScore = 0;

const playerSelection = ["Rock", "Paper", "Scissors"];
let computerSelection = computerPlay();

function playRound(playerSelection, computerSelection) {
    if (playerSelection.toLowerCase() == "rock" && computerSelection == thing2) {
        
        computerScore++;
        return "You lose! Rock loses to Paper";
    } else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing3) {
        
        playerScore++;
        return "You win! Rock beats Scissors";
    } else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing1) {
        playerScore++;
        return win = "You win! Paper beats Rock";
    } else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing3) {
        computerScore++;
        return win = "You lose! Paper loses to Scissors";
    } else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing1) {
        computerScore++;
        return win = "You lose! Rock loses Scissors";
    } else if (playerSelection.toLowerCase() == "scissors" && computerSelection == thing2) {
        playerScore++;
        return win = "You win! Scissors beats Paper!";
    } else {
        return "Tie!";
    }
}



function game() {
    let playerSelection = prompt("Rock, paper or scissors?");
    result = playRound(playerSelection, computerSelection);
    scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
    console.log(scoreboard);
    return result;
    

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You only call computerPlay() once, leading it computerSelection being the exact same every round. Call it every round to get a new random value.

function game() {
    let playerSelection = prompt("Rock, paper or scissors?");
    computerSelection = computerPlay(); // <---
    result = playRound(playerSelection, computerSelection);
    scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
    console.log(scoreboard);
    return result;
}
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!