I am trying to create a function called game()
that will prompt a user to input text of 'rock', 'paper', or 'scissor'. That input text will be assigned to const playerSelection
. Following that, a const computerSelection
will pull a random hand signal from my getComputerChoice()
function.
When those two values are evaluated, function playRound()
will be invoked passing the playerSelection
and computerSelection
arguments into playRound()
. However, the console is throwing an Uncaught ReferenceError: playerSelection is not defined
error in the first line of the playRound() function.
I thought defining the playerSelection
before the playRound()
is invoked would be correct, but I can't figure out why it's saying it's not defined.
I apologize if my code is unwieldy. I am a fresh novice. =]
function getComputerChoice() {
let computerChoice = Math.floor(Math.random() * 3);
if (computerChoice === 0) {
return 'rock';
} else if (computerChoice === 1) {
return 'paper';
} else if (computerChoice === 2) {
return 'scissor';
}
}
function playRound() {
if (playerSelection === computerSelection) {
return 'Draw!';
} else if (playerSelection === 'rock' && computerSelection === 'scissor') {
return 'You Win! Rock beats Scissor';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
return 'You Lose! Paper beats Rock';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
return 'You Win! Paper beats Rock';
} else if (playerSelection === 'paper' && computerSelection === 'scissor') {
return 'You Lose! Scissor beats Paper';
} else if (playerSelection === 'scissor' && computerSelection === 'paper') {
return 'You Win! Scissor beats Paper';
} else if (playerSelection === 'scissor' && computerSelection === 'rock') {
return 'You Lose! Rock beats Scissor';
} return 'Invalid input';
}
function game() {
const playerSelection = prompt("Choose hand signal", "");
const computerSelection = getComputerChoice();
console.log(computerSelection);
playRound()
}
game();