Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Uncaught reference even though variable is defined in previous function

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()

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!