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
  • 371,531
  • 55
  • 528
  • 584
tklein
  • 77
  • 1
  • 7
  • 3
    You don't assign the result of the second `prompt()` call back to `userChoice`, so `userChoice` never changes. – Pointy Feb 22 '16 at 15:09
  • 3
    ... + you're comparing against `"scissors"` only. – Teemu Feb 22 '16 at 15:10
  • 4
    `userChoice !== "rock", "paper", "scissors"` isn't [doing what you think it is](http://stackoverflow.com/questions/9579546/when-is-the-comma-operator-useful) – James Thorpe Feb 22 '16 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...?) – James Thorpe Feb 22 '16 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. – John Dvorak Feb 22 '16 at 15:20

1 Answers1

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")
    
Idos
  • 14,036
  • 13
  • 48
  • 65
  • 3
    An alternative is using `Array.indexOf`: `['rock', 'paper', 'scissors'].indexOf('no')` (which results in `-1`) – h2ooooooo Feb 22 '16 at 15:13
  • 1
    True and thanks for that, although probably too advanced for the OP who I assume is a beginner in JS – Idos Feb 22 '16 at 15:14
  • 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 Feb 22 '16 at 15:19
  • 1
    @jmcgriz not only too advanced, but also less readable and probably slower than the array approach. – John Dvorak Feb 22 '16 at 15:23
  • Thanks! And sorry for the newbie question, but I was really sweating it out. – tklein Feb 22 '16 at 15:30
  • @JanDvorak regex is only less readable if you don't take the time to learn how to read it. It's a separate language all to itself, but once you spend some time with it, it's perfectly logical. As for being slower, see for yourself http://jsperf.com/regexp-vs-indexof. In chrome, the regex test function is twice as fast as index of – jmcgriz Feb 22 '16 at 15:51