What is wrong with this code? Function to check user input (RPS)

javascript

#1

Hi!!! I'm new to javascript, i'm trying to make the game to ask for the user input again, if one of the three valid options is misspelled or something. Sort of works, but only one time.I don't understand why the while loop breaks after asking only one time to re-enter the text, even when the condition stored in valid is still false. Thanks!!!
Code below, JsBin here: http://jsbin.com/mitoziriqa/edit?js,console


//Ask for user input
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User: " + userChoice);
//check for correct input
var valid

function check(){
  if (userChoice === "rock" || userChoice === "paper" || userChoice === "scissors")
  {valid = true;}
  else {valid = false;}
} 

check();
console.log("the user input is " + valid)

//Ask for user input again, if input is incorrect
while (!valid){
  userChoice = prompt("input not valid, please re-enter text");
  console.log("new user input is: " + userChoice);    
  check();
  console.log("the new user input is " + valid);
}

//Computer choice
var computerChoice = Math.random();

if (computerChoice < 0.34)
{ computerChoice = "rock"; }
else if (computerChoice <= 0.67)
{ computerChoice = "paper"; }
else
{ computerChoice = "scissors"; }
console.log("Computer: " + computerChoice);


//Compare Choices
var compare = function (choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else (choice1 === "scissors");
    {
        if (choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
};

// Give Results
result = compare(userChoice, computerChoice);
console.log(result);


#2

Be sure to end this statement with a semi-colon.


#3

Thanks for the answer! Yeah, that was wrong, i corrected it, but don`t solve the issue, it still does the loop only one time...


#4

There is a built-in function, String.prototype.match that can help with this check. It returns an array of matches, or null when there is no match, so can be tested for falsiness.

Another method is RegExp.prototype.test that returns a boolean.

In either case, the regex will be,

var re = /rock|paper|scissors/i;
console.log(re.test('rock'));
console.log('rock'.match(re));

true
[ 'rock', index: 0, input: 'rock' ]

If you do not wish to use Regex, then there is the Array.prototype.indexOf method that returns an index if a match is found, or -1 if no match.

var userChoice;
do {
    userChoice = prompt("Do you choose rock, paper or scissors?");
} while (['rock', 'paper', 'scissors'].indexOf(userChoice) < 0);

#5

Thanks again!! I'll check that, but seems a bit over my head right now... i find this code, and it works, and can understand why it works, so i will use that. Still, i can figure out why my original code don't, i really like to know that...

var validate_input = function() {
    userChoice = prompt("make your choice: ");
    /* validate the user choice, if valid, return userChoice */
    if (userChoice === "rock") {
        return userChoice;
    } else if (userChoice === "paper") {
        return userChoice;
    } else if (userChoice === "scissors") {
        return userChoice;
    } else {
    /* invalid choice, the function need to run again */
    alert("Wrong choice, choose again");
    return validate_input();
    }
}
userChoice = validate_input();
console.log(userChoice);

#6

That will be left to you to decipher. Some reading is in order if you did not understand by earlier suggestions. You need to learn how to be critical of your own code.


#7

Yeah, you are totally right, and i'm grateful you took the time to answer my questions... i had the impression that it doesn't work because i'm mixing local with global variables... but i'm new to this, and i'm not sure... well, i'm gonna keep digging, and surely gonna check the method you suggest too. Thanks again mtf!!


#8

Projects are okay for practice, but I find them a rather poor way to learn. Go back and practice the basics, and hone your understanding of all the ins and outs of each concept. This sort of practice should be exhaustive, to the point where you dream it in your sleep. Master the little things and projects will come together more easily.


#9

Thanks Again!! Well, i listen to you, go back to the basics, and went on reading a bunch of things on the scope of functions and variables, but nothing help me to find the bug (but learn a lot of things), all seems right... So i try to test the code outside jsbin, in a .js file linked to an html in the browser... and WTF!!?? it works there!!!! So, don't know, seems my code always was ok, and only in jsbin the while loop breaks for no reason... Well... i write this only for give a closure to this, in case it's helpful for someone. Can't trust in jsbin seems...


#10

Don't blame the tools. The trust factor lands squarely at our own feet. In time you will come to realize that.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.