0

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()
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
MdCAL12
  • 3
  • 2
  • 1
    The `win` variable you declare inside `playRound` is **private** to the `playRound` code, and separately it no longer exists when `playRound` returns. If you want other code to use `win`, return its value from `playRound`, receive it in a variable in the scope where you're calling `playRound` (in `game`), and use it there. – T.J. Crowder Apr 28 '22 at 10:06
  • So it appears that the function is simply not acknowledging win and loss as a variable. I set playround equal to a variable and then checked to see if that variable was equal to win and still received "Uncaught ReferenceError: win is not defined" – MdCAL12 Apr 28 '22 at 10:19

0 Answers0