0

Homework help vampire alert. I'm trying to add a user input string validation function to the "Rock, paper, scissors" game on Codecademy. The text editor on the Codecademy site validates my code as correct, but I'm not getting the expected behavior, which is to return an alert if the user fails to enter either a "rock", "paper", or "scissors" string.

I've piped together the conditions in the if statement below:

var userChoice = prompt("Do you choose rock, paper or scissors?");

function validateUserChoice(userChoice) {
    if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
        alert("You can only select rock, paper or scissors!");
        return false;
    }
};

And here's the rest of the game. When the preceding function is invoked, it appears that the compare function below is bypassed and whatever string the user typed into the prompt is printed to the screen (see console.log("User Choice: " + userChoice); at the bottom). Otherwise, the game returns the expected behavior when the function above is commented out:

var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
};

var compare = function(userChoice, computerChoice) {
    if (userChoice === computerChoice) {
        return "The result is a tie!";
    };

    if (userChoice === "paper") {
        if (computerChoice === "rock") {
            return "paper wins";
        } else {
            if (computerChoice === "scissors") {
                return "scissors wins";
            }
        }   
    };

    if (userChoice === "scissors") {
        if (computerChoice === "rock") {
            return "rock wins";
        } else {
            if (computerChoice === "paper") {
                return "scissors wins";
            }
        }
    }
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);
2
  • 2
    (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") means if they didn't type rock it would validate as true. You want (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"), so if all of their choices were not valid. Commented Sep 22, 2015 at 14:58
  • Thanks for the syntax correction and explanation. Also, discovered a broader solution that demonstrates how to validate with a while loop here: stackoverflow.com/questions/31737867/… Commented Sep 22, 2015 at 15:39

1 Answer 1

3

Try changing the condition like below,

function validateUserChoice(userChoice) {
    if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
        alert("You can only select rock, paper or scissors!");
        return false;
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.