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
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.
Move your
computerScore
andplayerScore
vars outside of theplayerRound
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.Move your
computerScore
andplayerScore
vars to thegame()
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.
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
andcompuerScore
andplayerRound
will update those values. Instead of resetting theresult
variable I also justreturn
it after incrementing the score. This is a small optimisation, but it looks easier to read.So first I’d simplify/uniformize
playerRound
so that the return all start the same and match the requirements. I’d also ensureplayerChoice
is valid with a simple loop. Then with a more uniform return fromplayerRound
you can check aslice
to see if you win or lose and increment the correct score.