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

187
Views
Javascipt Rock Paper Scissors game, single mode or best out of three

I'm new to coding and javascript. I'm working on an assignment where I have to create a Rock Paper Scissors game that has 2 modes, single player and best out of three. In the best out of three mode, you need to create a system to remember the score of the user and the bot. And it needs a loop to run as many rounds as possible until there is a player that wins at least two rounds. Once the game ends, you can ask the human player if he/she wants to play again using a confirm() function. I have the base game but I cant figure out how to have the game loop until one player wins 2 rounds. I also cant figure out how to add a play again option. If someone can please help I would greatly appreciate it.

const play = () => {
  // set Computer Choice 
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }

  console.log("Player Choice: " + userChoice);
  console.log("Computer Choice: " + computerChoice);


  if (computerChoice === userChoice) {
    return "The result is tie!";
  }
  if (computerChoice === "rock") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "paper")
        return "Player wins";
    }
  }
  if (computerChoice === "paper") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "rock")
      return "Player wins";
  }
}
if (computerChoice === "scissors") {
  if (userChoice === "rock") {
    return "Computer wins";
  } else {
    if (userChoice === "scissors")
      return "Player wins";
  }
}
};
const round = () => {
const res = play();
let playerScore = 0;
let computerScore = 0;
console.log(res)
cnt--
wins[cnt] = res.startsWith("Player") ? 1 : 0;
if (cnt === 0) {
  const total = wins.reduce((a, b) => a + b)
  console.log(`You beat the computer ${total} time${total===1?"":"s"}`)
  return
}
setTimeout(round, 10) // else go again
}

let cnt = 1,
wins = [];
const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '2') {
  cnt = 3;

round()

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

0

You can pass a parameter to the round() function to tell it how many wins are needed. Then, put the main code in a loop:

const round = (winsNeeded) => {
  let playerScore = 0;
  let computerScore = 0;
  while (playerScore < winsNeeded && computerScore < winsNeeded) {
    const res = play();
    console.log(res)
    if (res[0] == 'P') playerScore++;
    else if (res[0] == 'C') computerScore++;
    console.log(`Score: you ${playerScore}, computer ${computerScore}`);
  }
  if (playerScore == winsNeeded) {
    console.log("Player won that round.");
  }
  else {
    console.log("Computer won that round.");
  }
}

while (true) {
  let cnt = 1;
  const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
  if (mode === '2') cnt = 2;
  round(cnt);
  const choice = prompt("Play again?");
  if (choice[0].toLowerCase() != 'y') break;
}
about 4 years ago · Juan Pablo Isaza Report

0

For every 2 round you can check for the current status of the game. Also made some changes on the play logic

  const play = () => {
      // set Computer Choice 
      var userChoice = prompt("Do you choose rock, paper or scissors?");
      var computerChoice = Math.random();
      if (computerChoice < 0.34) {
        computerChoice = "rock";
      } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
      } else {
        computerChoice = "scissors";
      }
    
      console.log("Player Choice: " + userChoice);
      console.log("Computer Choice: " + computerChoice);
    
    
      if (computerChoice === userChoice) {
        return "The result is tie!";
      }
      //little change in logic here----
    
      if (computerChoice === "rock") {
        if (userChoice === "scissors") {
          return "Computer wins";
        } else {
          if (userChoice === "paper")
            return "Player wins";
        }
      }
      if (computerChoice === "paper") {
        if (userChoice === "rock") {
          return "Computer wins";
        } else {
          if (userChoice === "scissors")
          return "Player wins";
      }
    }
    if (computerChoice === "scissors") {
      if (userChoice === "paper") {
        return "Computer wins";
      } else {
        if (userChoice === "rock")
          return "Player wins";
      }
    }
    };
    const round = () => {
    const res = play();
    let playerScore = 0;
    let computerScore = 0;
    console.log(res)
    cnt--
    wins[cnt] = res.startsWith("Player") ? 1 :res.includes("tie") ?-1:0;
    console.log(wins)
    if (cnt === 0) {
      const total = wins.filter((a) => a==1)
      console.log(`You beat the computer ${total.length} time${total.length===1?"":"s"}`)
      playAgain()
    }else if(cnt == 1){ // Check for every round
      var el2 = wins[2];
      var el1 = wins[1];
      if(el2 == el1 && el1!=-1){
        const total = wins.filter((a) => a==1)
        console.log(`You beat the computer ${total.length} time${total.length===1?"":"s"}`)
        playAgain()
      }
    }
    if(pa){
    setTimeout(round, 10) // else go again
    }else{
      prompt("Thank you for playing")
    }
    }
    let pa = true;
    let cnt = 1,
    wins = [];
    const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
    if (mode === '2')
      cnt = 3;
    round()
    
    function playAgain(){
      if(confirm('Do you want to play again ?')){
        pa = true
        cnt = 1
        wins = [];
        const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
        if (mode === '2') 
          cnt = 3;
    
        round()
      }else{
        pa=false
      }
    }
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!