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

202
Views
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 answers
Answer question

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 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!