1

I'm currently building a Rock, Paper, Scissors game using JS and HTML. I'm working on having the computer select rock, paper, or scissors randomly. The script is then supposed to print the computer choice to my user interface. When I try and test-run this, I get the following error: Uncaught ReferenceError: Cannot access 'computerChoice' before initialization

I've included some of my code below.

             let computerSelection = Math.random();
                if (computerSelection < 0.33) {
                    computerChoice = "rock";
                } else if (computerSelection < 0.66) {
                    computerChoice = "paper";
                } else {
                    computerChoice = "scissors";
                }
                let computerChoice = "";
vcable
  • 509
  • 1
  • 5
  • 17

1 Answers1

1

Because you first used the computerChoice variable then you defined it!!!

You have to change your code:

let computerSelection = Math.random();
let computerChoice = "";
if (computerSelection < 0.33) {
computerChoice = "rock";
} else if (computerSelection < 0.66) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
ABlue
  • 664
  • 6
  • 20