jbent02
jbent02

Reputation: 9

Problem with if statements, for rock paper scissors program

I keep getting “you chose Rock you chose Paper you chose Scissors” whenever I run the following code, and I don’t know why.

//if they haven't picked 1, 2, or 3, ask for input until they do

while (userSELECT != 1 && userSELECT != 2 && userSELECT != 3) {
  var userSELECT = prompt("1 = Rock | 2 = Paper | 3 = Scissors", "<>");
};

// 1 is rock
if (userSELECT = '1') {
  console.log("you chose Rock")
};

// 2 is paper
if (userSELECT = '2') {
  console.log("you chose Paper")
};

// 3 is scissors
if (userSELECT = '3') {
  console.log("you chose Scissors")
};

Upvotes: 0

Views: 39

Answers (1)

Tamas Szoke
Tamas Szoke

Reputation: 5542

The = is for setting a value and not for comparison, so you should use == instead of =:

//if they haven't picked 1, 2, or 3, ask for input until they do

while (userSELECT != 1 && userSELECT != 2 && userSELECT != 3) {
  var userSELECT = prompt("1 = Rock | 2 = Paper | 3 = Scissors", "<>");
};

// 1 is rock
if (userSELECT == '1') {
  console.log("you chose Rock")
};

// 2 is paper
if (userSELECT == '2') {
  console.log("you chose Paper")
};

// 3 is scissors
if (userSELECT == '3') {
  console.log("you chose Scissors")
};

Upvotes: 1

Related Questions