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

193
Views
Random result and add final message on Rock, Paper, Scissors?

I have one or two questions about how to "randomize" a result on this game when you type the exact same choice.

e.g: I'm calling the function 5 times and if I type different choices (first rock, then paper and then scissors, etc..) it's okay, it throws different random results, but if I type one of them three or five times, like for e.g, paper, it will throw the same result everytime no matter what, and I would like to have different results on it, is this possible without using loops? I'm following TOP courses and I would like not to use loops until I get to that part.

Also, I would like to know if there is some way to print "Machine wins" or "Human wins" after all the attemps, depending on who wins, like a final message for winning or losing all rounds.

Here is the code:

function computerPlay() {
    const choice = ["rock", "paper", "scissors"];
    const choiceRandom = choice[Math.floor(Math.random() * choice.length)];
    return choiceRandom;
}

function game(computerSelection, playerSelection) {

    playerSelection = prompt("Rock, paper or scissors?").toLowerCase();

    if (computerSelection=="rock" && playerSelection=="scissors") {
        console.log("You lose, rock beats scissors");
    } else if (computerSelection=="paper" && playerSelection=="scissors") {
        console.log("You win, scissors beats paper!");
    } else if (computerSelection=="rock" && playerSelection=="paper") {
        console.log("You lose, rock beats paper!");
    } else if (computerSelection=="paper" && playerSelection=="rock") {
        console.log("You lose, paper beats rock!");
    } else if (computerSelection=="scissors" && playerSelection=="rock") {
        console.log("You win, rock beats scissors!");
    } else if (computerSelection=="scissors" && playerSelection=="paper") {
        console.log("You lose, scissors beats paper!");
    } else if (computerSelection===playerSelection) {
        console.log("Draw!");
    }
}

const computerSelection = computerPlay();
let playerSelection

game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);
game(computerSelection, playerSelection);

I'm blank at this, have been trying to figure it out for hours.

Thank you.

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

0

Regarding you question on randomness, it looks like you are setting your computer's selection to a constant value passing it to your game function - meaning it will always be the same!

If you place both the computerSelection and playerSelection in the game function the computerSelection will call the computerPlay function each time you start a game. As for the player selection, it belongs in the function because that is as far the scope needs to extend.

Finally, you can wrap the inside of you game function in a for loop and declare two variables machineWins and playerWins. Increment the wins for each. Then at the end of the function you can have an if else statement that will log your message based on who has the most wins.

game() {
  let machineWins = 0;
  let playerWins = 0;
  for (let gamesPlayed = 0; gamesPlayed < 5; gamesPlayed++) {
   // game logic, if computer wins machineWins++, else playerWins++
  }
  if (machineWins > playerWins) {
   // log machine wins
  } else {
   // log player wins
  }
}
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!