0

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")
};
Sebastian Simon
  • 14,320
  • 6
  • 42
  • 61
  • 1
    `userSELECT = '1'` here you are assigning values , change it to `userSELECT = =='1'` , so for other if and else conditions – brk May 05 '20 at 12:53
  • 2
    Learn the difference between `=`, `==`, and `===`. Please try using the [debugging capabilities](https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser. Use tools like [JSHint](http://jshint.com/) to find problems with your code immediately. See [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators). – Sebastian Simon May 05 '20 at 12:53

1 Answers1

1

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")
};
Tamas Szoke
  • 4,128
  • 3
  • 19
  • 30
  • Of course, welcome to Stack Overflow! If you feel an answer solved the question, please mark it as accepted by clicking on the check mark next to it. This helps to keep the focus on unanswered questions. – Tamas Szoke May 05 '20 at 13:05