Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
How do I get the return of a result from a Win or loss in Rock paper scissors javascript

How would I update the game score after each result? This is something i've been working on in console as part of a school project and spent way too much time trying to tie this up. I have managed to get the results to show on the console as who is the winner based on the switch and cases but how do I take that extracted outcome of string text and make something that will update the game array? Sorry if i'm not asking the right question

const game = {
  win: 0,
  loss: 0,
};

const choices = ["rock", "paper", "scissors"];

var random1 = Math.round(Math.random() * 2);
var random2 = Math.round(Math.random() * 2);

var bot1 = choices[random1];
var bot2 = choices[random2];

console.log("Player 1:" + bot1);
console.log("Player 2:" + bot2);

var result = "";

function returnResult() {
  switch (bot1) {
    case "paper":
      switch (bot2) {
        case "rock":
          console.log("Player 1 Wins!");
          result = win++;
          break;
        case "scissors":
          console.log("Player 2 Wins!");
          result = loss++;
          break;
        case "paper":
          console.log("Draw!");
          break;
      }
  }

  switch (bot1) {
    case "rock":
      switch (bot2) {
        case "scissors":
          console.log("Player 1 Wins!");
          result = win++;
          break;
        case "paper":
          console.log("Player 2 Wins!");
          result = loss++;
          break;
        case "rock":
          console.log("Draw!");
          break;
      }
  }

  switch (bot1) {
    case "scissors":
      switch (bot2) {
        case "paper":
          console.log("Player 1 Wins!");
          result = win++;
          break;
        case "rock":
          console.log("Player 2 Wins!");
          result = loss++;
          break;
        case "scissors":
          console.log("Draw!");
          break;
      }
  }
  return result;
}
console.log(results);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Not sure how you intend on playing the game, but here is a suggestion on how to keep score in your game object. Press the Play button to start game and keep score in game:

const game = {
  win: 0,
  loss: 0,
};

const choices = ["rock", "paper", "scissors"];

function returnResult() {
  console.clear() // This is just to prevent console crowding for this example

  // Move your random number generators inside the function
  // so you get a new random number each time game is played.
  const random1 = Math.round(Math.random() * 2);
  const random2 = Math.round(Math.random() * 2);

  const bot1 = choices[random1];
  const bot2 = choices[random2];

  console.log("Player 1:" + bot1);
  console.log("Player 2:" + bot2);

  // var result = "";  You don't this if you are keeping score in `game` object

  // No need to call switch on bot1 three times:
  switch (bot1) {
    case "paper":
      switch (bot2) {
        case "rock":
          console.log("Player 1 Wins!");
          game.win++;  // Update game win
          break;
        case "scissors":
          console.log("Player 2 Wins!");
          game.loss++; // Update game loss
          break;
        case "paper":
          console.log("Draw!");
          break;
      }
      break;

    case "rock":
      switch (bot2) {
        case "scissors":
          console.log("Player 1 Wins!");
          game.win++; // Update game win
          break;
        case "paper":
          console.log("Player 2 Wins!");
          game.loss++; // Update game loss
          break;
        case "rock":
          console.log("Draw!");
          break;
      }
      break;

    case "scissors":
      switch (bot2) {
        case "paper":
          console.log("Player 1 Wins!");
          game.win++; // Update game win
          break;
        case "rock":
          console.log("Player 2 Wins!");
          game.loss++; // Update game loss
          break;
        case "scissors":
          console.log("Draw!");
          break;
      }
      break;
  }

  // return result;  No need to return result.  Update game object every play instead.
  console.log(game)
  return game // Return game object if needed
}
<button onclick="returnResult()">Play</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda