How to re-prompt the question"pick rock, paper,..." if dog is typed


#1

How do I reroute the function to go to line 1 where my prompt("Do you choose rock, paper, or scissors?") is? This would be useful if the user typed in words other than "rock", "paper", or "scissors". So, basically I want it to keep asking "You must pick either rock, paper, or scissors." if they type in random words until they enter in one of the three.


#2

The solution you need requires an understanding of loop constructs which may not yet have been covered. You can always set this aside for now and return later once you are better equipped. It will save a lot of run around.


#3

function user() {
    var re = /rock|paper|scissors/i;
    var u = prompt("rock, paper or scissors?");
    if (re.test(u)) {
        return u.toLowerCase();
    }
    return null;
}
var userChoice = user();

The above is a sketch illustrating our generalized test. The function either returns one of rock, paper or scissors or it returns null. There is no other possible return value. Now we need to put this into a loop so that the function only returns null if the user wants to quit. Let us know when you are ready to tackle this next step.


#4

Thank you for clarifying this. I have not learned about loops yet but that makes sense.


#5

Ok. Thank you for showing the steps. I will work on it as soon as I learn more about loops!