everyone. This is probably a simple question but I cannot figure it out. In my second function ,function game(), I want the valued returned from my first function,playRound(p,c), however when the code gets down to the if statement asking if the returned value from playround is equal to win I get get an uncaught reference error saying that win is not defined. However, win is defined in the playround function.
const choices = ['rock', 'paper', 'scissors']
function computerPlay(computer) {
let compPick = Math.floor(Math.random() * choices.length);
// math.random gives random number between 0-1, (1 exclusive)
// length of the array is 3. Length is always n+1 the highest index in the array.
// math.floor rounds down
// math.floor(0<n<3). So n will be between 0 and 2, which are the index's of the array.
return choices[compPick]
}
// let computerSelection = computerPlay(choices) // computers pick
// let playerSelection = prompt("Rock, Paper, or Scissors?") //asks player to pick
// playerSelection = playerSelection.toLowerCase();
//console.log(playerSelection)
function playRound(p, c) {
//console.log(p)
//console.log(c)
let win = 'You win ' + p + ' beats ' + c
let loss = 'You lose ' + c + ' beats ' + p
let tie = 'You tied ' + p + ' equals ' + c
while (p == 'rock') {
if (c == 'rock') {
return (tie)
} else if (c == 'paper') {
return (loss)
} else {
return (win)
}
break;
}
while (p == 'paper') {
if (c == 'rock') {
return (win)
} else if (c == 'paper') {
return (tie)
} else {
return (loss)
}
break;
}
while (p == 'scissors') {
if (c == 'rock') {
return (loss)
} else if (c == 'paper') {
return (win)
} else {
return (tie)
}
break;
}
}
function game() {
//console.log (playRound(playerSelection, computerSelection))
let wins = 0
let losses = 0
let ties = 0
for (let i = 0; i < 5; i++) {
let computerSelection = computerPlay(choices)
let playerSelection = prompt("Rock, Paper, or Scissors?")
playerSelection = playerSelection.toLowerCase();
playRound(playerSelection, computerSelection)
if (playRound == win) {
wins += 1;
} else if (playRound == loss) {
losses += 1;
} else {
ties += 1;
}
}
console.log(wins)
console.log(losses)
console.log(ties)
}
// console.log (playRound(playerSelection, computerSelection))
game()