Adding my own code to rock paper scissors


#1

I'm trying to make the game not accept answers other than rock, paper or scissors. Do I have to use a loop to get the userChoice prompt to stay the same as it is in the begining? The way I have it now, what ever the input is is what the userChoice prompt becomes the next time around.

var userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice != "rock", "paper", "scissors") {
    confirm(userChoice + " " + "is not a valid response");
    prompt(userChoice);
};

//^^^^^^^^ this is the bit I'm having trouble with^^^^^^^^^//


var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
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 if(choice1 === "scissors") {
        if(choice2 === "paper") {
            return "scissors wins"
        }
        else {
            return "rock wins"
        }
    }   
};
compare(userChoice, computerChoice);



#2

My advice to you would be to first study arrays, the code would then be easily written as:

var validResponses = ["rock", "paper", etc];

if(validResponses.indexOf(userChoice) == -1){
    // punish the user
}

or you could even try using a switch-case:

switch(userChoice){
    case "rock": // code
        break;
    case "scissors": // code
        break;
    case "paper": // code
        break;
    default: // did not match any of the above choices
        // punish the user
}

The first method is the best method as it is easily extensible (just add a new element to that array), but if you really do want to use an if condition, it would be like this:

if( userChoice !== "rock" &&
    userChoice !== "scissors" &&
    userChoice !== "paper"){
    // punish the user
}

P.S. The method you are currently using invokes the comma operator. You can read more.


#3

This is how I would do it:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var upperUserChoice = userChoice.toUpperCase();
if (upperUserChoice != 'ROCK' && upperUserChoice != 'PAPER' && upperUserChoice != 'SCISSORS') { 
    confirm(userChoice + " " + "is not a valid response");
    userChoice = prompt('Do you choose rock, paper or scissors?');
};

Of course, it only asks again once, before effectively 'giving up', but I'll take another look at it tomorrow. (Actually later, because it's 4:35AM here so I'm logging off now :stuck_out_tongue: )


#4

There's the code :slight_smile:

var userChoice;
while(userChoice !== "rock" && userChoice !== "scissors" && userChoice !== "paper"){
    userChoice = prompt();
}
// do-while is better though

#5

Having the same issue, thanks for all the help and OP. My code is very similar, if not almost identical with OP except for the comma operator, I used plus signs instead.

Can someone explain why using just that if loop (!= "rock", "papers", "scissors"), if given an invalid input, only prompts the user again once?

Why doesn't it force the user to try again and again via prompt until it receives an answer that satisfies the if loop (rock, paper, or scissors) and then lets the rest of the program move on? Why wouldn't it be infinite?

Apologies if this is very elementary. And is this thinking due to a skewed/incorrect conceptual understanding of JS?


#6

Please note that highlights I have put.

if is a selection statement. Given an statement-block like this:

if(condition){
    // code
}
// more code

It will evaluate the condition and if it equals true, it will execute the statement-block. But, if it is false, it will skip the statement-block and move to // more code lines.

if is NOT a loop. It only tests the condition once, and executes the statement-block, if at all, once.

That is why, to prompt the user multiple times (not once) until we received valid input, I had to use a loop:

while(userChoice !== "rock" && userChoice !== "scissors" && userChoice !== "paper"){
    userChoice = prompt();
}

I hope it helps! :slight_smile:


"between 0.67 and 1" -- achievable with current knowledge?
#7

3 posts were merged into an existing topic: "between 0.67 and 1" -- achievable with current knowledge?