skip to Main Content

I am working on a coding task to coding a Rock, Paper, Scissors game.
I have written some functions already to handle to computer choice and user choice, and I’m also supposed to write a function to call the other functions and also keep track of the scores.

I am stuck without a way of bring everything together. This is my code below.

let input;
let random;

function getComputerChoice(min = 1, max = 3){
    min = Math.ceil(min);
    max = Math.floor(max);
    random = Math.floor(Math.random() * (max - min + 1) + min);

    if (random === 1){
        console.log("computer picked Rock");
        return random;
    }
    else if (random === 2){
        console.log("computer picked Paper");
        return random;
    }
    else
        random === 3;
        console.log("computer picked Scissors");
        return random;

}


function playerSelection(){
    input = prompt("Input Rock, Paper or Scissors").toLowerCase();  
    console.log("you picked", input);
    return input;
} 


function playRound(yourSelection, computerSelection) {
    // your code here!
    
    if (yourSelection === "rock" && computerSelection === 3){
        console.log("you win")
    }
    else if (yourSelection === "scissors" && computerSelection === 2){
        console.log("you win")
    }
    else if (yourSelection === "paper" && computerSelection === 1){
        console.log("you win")
    }
    else
        console.log("You lose, try again")
}
   
const yourSelection = playerSelection();
const computerSelection = getComputerChoice();
playRound(yourSelection, computerSelection);

function game(){
    var theGame = game()
}

2

Answers


  1. It looks like you’re off to a good start with your rock-paper-scissors game, but there are some issues in your code that need to be addressed. Here is a corrected version of your code:

    let random;
    
    function getComputerChoice(min = 1, max = 3) {
      min = Math.ceil(min);
      max = Math.floor(max);
      random = Math.floor(Math.random() * (max - min + 1) + min);
    
      if (random === 1) {
        console.log("computer picked Rock");
        return "rock";
      } else if (random === 2) {
        console.log("computer picked Paper");
        return "paper";
      } else {
        console.log("computer picked Scissors");
        return "scissors";
      }
    }
    
    function playerSelection() {
      const input = prompt("Input Rock, Paper, or Scissors").toLowerCase();
      console.log("you picked", input);
      return input;
    }
    
    function playRound(yourSelection, computerSelection) {
      if (yourSelection === computerSelection) {
        console.log("It's a tie!");
      } else if (
        (yourSelection === "rock" && computerSelection === "scissors") ||
        (yourSelection === "scissors" && computerSelection === "paper") ||
        (yourSelection === "paper" && computerSelection === "rock")
      ) {
        console.log("You win!");
      } else {
        console.log("Computer wins!");
      }
    }
    
    function game() {
      let playerScore = 0;
      let computerScore = 0;
      let rounds = 5;
    
      for (let i = 0; i < rounds; i++) {
        const yourSelection = playerSelection();
        const computerSelection = getComputerChoice();
        playRound(yourSelection, computerSelection);
    
        if (yourSelection === computerSelection) {
          // It's a tie; no one scores.
        } else if (
          (yourSelection === "rock" && computerSelection === "scissors") ||
          (yourSelection === "scissors" && computerSelection === "paper") ||
          (yourSelection === "paper" && computerSelection === "rock")
        ) {
          playerScore++;
        } else {
          computerScore++;
        }
    
        console.log("Player Score: " + playerScore);
        console.log("Computer Score: " + computerScore);
      }
    
      if (playerScore > computerScore) {
        console.log("You won the game!");
      } else if (playerScore < computerScore) {
        console.log("Computer won the game!");
      } else {
        console.log("It's a tie game!");
      }
    }
    
    game();
    Login or Signup to reply.
  2. In your game function you’d need a loop to run a round and save the scores. If you want to end the game enter "exit".

    function getComputerChoice(min = 1, max = 3) {
      min = Math.ceil(min);
      max = Math.floor(max);
      let random = Math.floor(Math.random() * (max - min + 1) + min);
    
      if (random === 1) {
        console.log("computer picked Rock");
        return random;
      } else if (random === 2) {
        console.log("computer picked Paper");
        return random;
      } else
        random === 3;
      console.log("computer picked Scissors");
      return random;
    }
    
    function playerSelection() {
      let input = prompt("Input Rock, Paper or Scissors or exit to end the game").toLowerCase();
      console.log("you picked", input);
      return input;
    }
    
    function playRound(yourSelection, computerSelection) {
      if (yourSelection === "rock" && computerSelection === 3) {
        console.log("you win")
        return true;
      } else if (yourSelection === "scissors" && computerSelection === 2) {
        console.log("you win")
        return true;
      } else if (yourSelection === "paper" && computerSelection === 1) {
        console.log("you win")
        return true;
      } else
        console.log("You lose, try again")
      return false;
    }
    
    function game() {
      let yourScore = 0,
        computerScore = 0;
      while (true) {
        const yourSelection = playerSelection();
        if (yourSelection === 'exit') break;
        const computerSelection = getComputerChoice();
        const youWon = playRound(yourSelection, computerSelection);
        if (youWon) {
          yourScore += 1;
        } else {
          computerScore += 1;
        }
        alert(`${youWon ? 'You won!' : 'Computer won!'} your score=${yourScore}, computerScore=${computerScore}`)
      }
    }
    game();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search