Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
use my 1 round code to make a 3 rounds "Rock Paper Scissors" game - Javascript

I'm building a rock paper scissors game with Javascript, i did a 1 round mode of the game and it work, now i want to add a second mode " best of three rounds" but after trying many things i got my code messy and can't figure out what to do exactly.

i try to add a count = 0; count < 3; count = count + 1but don't know exactly where to put it

can someone help me please?

var mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '1') {
  oneRound;
}
if (mode === '2') {
  bestOfThree;
}

// 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";
}

// Compare value for oneRound mode


var oneRound = function(computerChoice, userChoice) {
  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";
    }
  }

};

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Have look at this

I use setTimeout to allow the screen to show the result

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();
  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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda