0

I've been trying to fix this loop, but apparently it doesn't recognize when the input is right. Am I using the right kind of loop here?

var userChoice = prompt("Do you choose rock, paper or scissors?") 
if (userChoice !== "rock", "paper", "scissors") {
 do {
 prompt("Invalid answer. Please choose rock, paper or scissors.");
 } while (userChoice !== "rock", "paper", "scissors");
} else { 
Pointy
415k62 gold badges600 silver badges633 bronze badges
asked Feb 22, 2016 at 15:07
5
  • 3
    You don't assign the result of the second prompt() call back to userChoice, so userChoice never changes. Commented Feb 22, 2016 at 15:09
  • 3
    ... + you're comparing against "scissors" only. Commented Feb 22, 2016 at 15:10
  • 4
    userChoice !== "rock", "paper", "scissors" isn't doing what you think it is Commented Feb 22, 2016 at 15:10
  • 2
    I'm also going to preemptively guess that your next question will be why the code in the else block only runs when they make a correct choice first time - you probably don't need the else at all, with the code in that block immediately following the if block instead. (unless it contains code to congratulate them on making a correct choice first time...?) Commented Feb 22, 2016 at 15:14
  • @JamesThorpe in short, the question is pretty much too broad, as good answers would have to fix all of the issues, and it's also caused by a "typo" and a partial duplicate of the question you linked. Commented Feb 22, 2016 at 15:20

1 Answer 1

6

There are 2 major issues here:

  • You need to re-assign userChoice inside your do-while loop:

    userChoice = prompt("Invalid answer. Please choose rock, paper or scissors.");
    
  • Your comparison is off and won't work, use this instead in your if and while:

    (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors")
    
answered Feb 22, 2016 at 15:10
Sign up to request clarification or add additional context in comments.

6 Comments

An alternative is using Array.indexOf: ['rock', 'paper', 'scissors'].indexOf('no') (which results in -1)
True and thanks for that, although probably too advanced for the OP who I assume is a beginner in JS
Also probably too advanced, but regex works well here as well var userChoice = prompt("Do you choose rock, paper or scissors?") ; var testrgx = /^(rock|paper|scissors)$/; if (testrgx.test(userChoice)){ } else { //while loop in here }
@jmcgriz not only too advanced, but also less readable and probably slower than the array approach.
Thanks! And sorry for the newbie question, but I was really sweating it out.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.