Advanced Rock Paper Scissors help!


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


Help! I am trying to play around with the code a bit for the Rock Paper Scissors game and trying to make it repeat if you get a tie or input an invalid value. When it ties I have it prompt again for the userChoice which works fine, except it doesn't redo the computerChoice; it will continue to use the same choice into infinity. I figured calling the variable function would work but it doesn't seem to.

Also, if an invalid value is input I have the program prompt for a new value. It will accept the new, correct value but will not run and print the code to the console. I think I'm really just having trouble recalling the compare function and that these two problems are the same/similar.

Thanks!


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
};

console.log("Computer has chosen: " + computerChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        userChoice = prompt("The result is a tie! Please choose again!");
        computerChoice;
        compare(userChoice, computerChoice);
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        };
    } else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            return "scissors wins";
        } else {
            return "paper wins";
        };
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins"; 
        } else {
            return "scissors wins";
        };
    } else {
        userChoice = prompt("Sorry, that is an invalid input. Please choose 'rock', 'paper', or 'scissors'.");
        compare(userChoice, computerChoice);
    };
};

compare(userChoice, computerChoice);


#2

first, remover all unnecessary semi-colons.

closing curly brackets (}) of if,else ifandelse shouldn't have semi-colons

i would place computerChoice picking a option inside a separate function, so you can call it when you need a new computerChoice, this you can also use in case the result is a tie


#3

Thanks for your suggestion! I tried placing computerChoice in a separate function but now when it ties it won't run the original compare function. It will print out the computer result but it won't run through the compare function and print out the results of the match. Any ideas as to what I'm doing wrong? I've tried everything I can think of.

var computerChoice; 
function computerHand() {
    computerChoice = Math.random();
    
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "paper";
    };
    console.log("Computer has chosen: " + computerChoice);
}
computerHand();


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

function compare(choice1, choice2) {
    if (choice1 === choice2) {
        userChoice = prompt("The result is a tie! Please choose again!");
        computerHand();
        compare(userChoice, computerChoice);
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            return "scissors wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins"; 
        } else {
            return "scissors wins";
        }
    } else {
        userChoice = prompt("Sorry, that is an invalid input. Please choose 'rock', 'paper', or 'scissors'.");
        compare(userChoice, computerChoice);
    }
};

compare(userChoice, computerChoice);

#4

if the case is a tie, you call the function again:

compare(userChoice, computerChoice);

you need to use return:

return compare(userChoice, computerChoice);

same here:

userChoice = prompt("Sorry, that is an invalid input. Please choose 'rock', 'paper', or 'scissors'.");
return compare(userChoice, computerChoice);

if you don't use return here, the function would return None (the default) and the original functional call can't access the winning string (rock wins)


#5

Oh my goodness now I feel stupid. Thank you so much!


#6

that is not stupid, function have been taught up to this point, what you do here is recursive functions, which hasn't been taught, and is a bit more advanced


#7

I see. I'm still having trouble getting it to work. With the addition of the return keyword it will now cycle through the computerHand() function if it is a tie but will still not run the compare() function. Should I stop fretting this game and just move on for now do you think?


#8

Can i see a your code again?

You can always move on and come back later


#9

Here's the most recent code:

var computerChoice; 
function computerHand() {
    computerChoice = Math.random();
    
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "paper";
    };
    console.log("Computer has chosen: " + computerChoice);
}
computerHand();


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

function compare(choice1, choice2) {
    if (choice1 === choice2) {
        userChoice = prompt("The result is a tie! Please choose again!");
        return computerHand();
        return compare(userChoice, computerChoice);
    } else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "paper") {
        if (choice2 === "scissors") {
            return "scissors wins";
        } else {
            return "paper wins";
        }
    } else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins"; 
        } else {
            return "scissors wins";
        }
    } else {
        userChoice = prompt("Sorry, that is an invalid input. Please choose 'rock', 'paper', or 'scissors'.");
        return compare(userChoice, computerChoice);
    }
};

compare(userChoice, computerChoice);

#10

i put your code in a bin

It seems to be fine now.

i made computerHand return computerChoice so we can store it in a global computerChoice variable, and that we can update computerChoice when the result is a tie


#11

Interesting. I tried doing that and it didn't work inside the codecademy browser/lesson page but it definitely works in the bin. I wonder why that is.

Thanks for all your help though! I really appreciate it!


#12

i did make some changes to the code? That does make a differences


#13

I see! I didn't see that you had added the "computerChoice = computerHand()" in the if statement. That makes much more sense now. Thanks again!


#14

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