skip to Main Content

The game is going to play against the computer, so begin with a function called getComputerChoice that will randomly return either ‘Rock’, ‘Paper’ or ‘Scissors’. We’ll use this function in the game to make the computer’s play.

Write a function that plays a single round of Rock Paper Scissors. The function should take two parameters – the playerSelection and computerSelection – and then return a string that declares the winner of the round like so: "You Lose! Paper beats Rock"

Write a NEW function called game(). Use the previous function inside of this one to play a 5 round game that keeps score and reports a winner or loser at the end.

I have done everything well but I get stuck in the last step which was the incrementation part.
Here is the code:

console.log("Rock Paper Scissor Game");

function getComputerChoice() {
  let words = ["rock", "paper", "scissor"];
  let n = Math.floor(Math.random() * words.length);
  return words[n];
}

function playerRound(playerSelection, computer) {
  let result;
  let computerScore;
  let playerScore;
  let player = playerSelection.toLowerCase();
  if (player === computer) {
    result = "Draw Match";
    computerScore = computerScore;
    playerScore = playerScore;
  } else if (player === 'rock' && computer === 'paper') {
    result = "You Lose ... 'Paper beats Rock'";
    computerScore += 1;
  } else if (player === 'rock' && computer === 'scissor') {
    result = "You Win!!! Rock breaks the scissor";
    playerScore += 1;
  } else if (player === 'paper' && computer === 'rock') {
    result = "Hurray you win!! Paper covers the rock";
    playerScore += 1;

  } else if (player === 'paper' && computer === 'scissor') {
    result = "Oh no you lose.. Scissor cuts the paper";
    computerScore += 1;
  } else if (player === 'scissor' && computer === 'rock') {
    result = "You lose .. Rock breaks the scissor";
    computerScore += 1;
  } else if (player === 'scissor' && computer === 'paper') {
    result = "You win!! Scissor cut the paper";
    playerScore += 1;
  } else {
    result = "You entered wrong  choice";
  }
  return result;
}

function game() {
  for (let i = 0; i < 5; i++) {
    const playerChoice = prompt("Enter your choice:n");
    const computerChoice = getComputerChoice();
    console.log(playerRound(playerChoice, computerChoice));
  }
}

game();

3

Answers


  1. You are storing the score in playerRound, but this is not a class, and therefore the variables are reset each function call.

    You have a couple of options.

    1. Move your computerScore and playerScore vars outside of the playerRound function so they sit inside the top level JS file. This way they will be persisted between function calls, and will require no other changes to your code.

    2. Move your computerScore and playerScore vars to the game() function, and pass some sort of state (e.g. true for player win, false for computer win), which you can then handle and increment the correct score appropriately.

    I would recommend option one as it will require the least changes to your code.

    Login or Signup to reply.
  2. You’ll have to organise your code a little differently so you can store the value in a more global scope. I have moved your game() function to wrap everything, and run the rounds inside of that individually.

    Inside that, we will store playScore and compuerScore and playerRound will update those values. Instead of resetting the result variable I also just return it after incrementing the score. This is a small optimisation, but it looks easier to read.

    function game(){
    
      function getComputerChoice() {
      
        const words = ["rock", "paper", "scissor"];
        const n = Math.floor(Math.random() * words.length);
        
        return words[n];
        
      }
    
      function playerRound( playerSelection, computer ){
      
        let player = playerSelection.toLowerCase();
        
        if (player === computer) {
          return "Draw Match";
        } else if (player === 'rock' && computer === 'paper') {
          computerScore += 1;
          return "You Lose ... 'Paper beats Rock'";
        } else if (player === 'rock' && computer === 'scissor') {
          playerScore += 1;
          return "You Win!!! Rock breaks the scissor";
        } else if (player === 'paper' && computer === 'rock') {
          playerScore += 1;
          return "Hurray you win!! Paper covers the rock";
        } else if (player === 'paper' && computer === 'scissor') {
          computerScore += 1;
          return "Oh no you lose.. Scissor cuts the paper";
        } else if (player === 'scissor' && computer === 'rock') {
          computerScore += 1;
          return "You lose .. Rock breaks the scissor";
        } else if (player === 'scissor' && computer === 'paper') {
          playerScore += 1;
          return "You win!! Scissor cut the paper";
        }
        
        return "You entered wrong  choice";
      }
    
      let computerScore = 0;
      let playerScore = 0;
        
      for (let i = 0; i < 5; i++) {
        const playerChoice = prompt("Enter your choice:n");
        const computerChoice = getComputerChoice();
        console.log( playerRound(playerChoice, computerChoice) );
      }
      
      console.log({results: {
        computerScore,
        playerScore
      }})
    
    }
    
    game();
    Login or Signup to reply.
  3. So first I’d simplify/uniformize playerRound so that the return all start the same and match the requirements. I’d also ensure playerChoice is valid with a simple loop. Then with a more uniform return from playerRound you can check a slice to see if you win or lose and increment the correct score.

    console.log("Rock Paper Scissor Game");
    
    function getComputerChoice() {
      let words = ["rock", "paper", "scissor"];
      let n = Math.floor(Math.random() * words.length);
      return words[n];
    }
    
    function playerRound(playerSelection, computer) {
      let result;
      let player = playerSelection.toLowerCase();
      if (player === computer) {
        result = "Draw Match";
      } else if ((player === 'rock' && computer === 'paper') ||
                 (player === 'paper' && computer === 'scissors') ||
                 (player === 'scissors' && computer === 'rock')) {
        result = `You Lose ... '${computer} beats ${player}'`;
      } else {
        result = `You Win!!! ${player} beats ${computer}`;
      }
      return result;
    }
    
    function game() {
      let playerScore=0;
      let computerScore=0;
      for (let i = 0; i < 5; i++) {
        let playerChoice = ''
        while(!['rock','paper','scissors'].includes(playerChoice.toLowerCase())){
            playerChoice = prompt("Enter your choice:n");
        }
        const computerChoice = getComputerChoice();
        const round = playerRound(playerChoice, computerChoice);
        if (round.slice(0,5) === 'You L') {
           computerScore += 1;
        } else if (round.slice(0,5) === 'You W') {
           playerScore+=1;
        }
        console.log(round);
      }
      if (playerScore>computerScore) {
         console.log('You win the round');
      } else if (playerScore<computerScore) {
         console.log('You lose the round');
      } else {
        console.log("It's a draw");
      }
    }
    
    game();
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search