9. Next Steps - Rock, Paper, Scissors


#1


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


So I attempted a few of the suggestions by Codecademy for further building out our Rock, Paper, & Scissors game. I was unfortunately unsuccessful!

In it, if there was a tie, I tried to rerun the compare function again within the compare function. Can I even do that? It didn't work. When there was a tie, it just returns my text to the console but no further prompts. Thoughts on how I would do this?

The second thing I attempted to do was to prohibit a player from making an inappropriate choice (ie. not rock, paper or scissor). I think I would need a loop to keep this going, but I'm not sure. I was able to stop the initial attempt with an inaccurate answer, but on the second prompt it just moves forward. Would I need a loop? Thoughts?

Any help would be greatly appreciated. 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: " + computerChoice);

var compare = function(userChoice, computerChoice) {
    if(userChoice === computerChoice) {
        return "Can't tie, must try again!";
        compare(userChoice, computerChoice);
    }
    else if(userChoice === "rock") {
        if(computerChoice === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
        }
    }
        else if(userChoice === "paper") {
            if(computerChoice === "rock") {
                return "paper wins";
            } else {
                return "scissors win";
            }
        }
            else if(userChoice === "scissors") {
                if(computerChoice === "rock") {
                    return "rock wins";
            } else {
                return "scissors win";
            }
        }
        else if(userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
    confirm("That is not a choice");
    prompt("Do you choose rock, paper or scissors?");
    

};
};

compare(userChoice, computerChoice);


#2

In your code, where you're trying to run your compare function again, that part of your code is not being read. A function exits as soon as the return function executes. Anything below that return function will not run.

Also, I took another route for the inappropriate choice. Instead of trying to write the logic of when things are not equal to rock, paper, or scissors, I just wrote and else statement at the bottom without using any !== comparisons. Since your code will execute line by line all cases of correct choices (i.e. "rock", "paper", "scissors") are taken care of already. Therefore anything else written in to the prompt box is an invalid answer.

else {
return userChoice + " " + "is not a known weapon. Please select an appropriate weapon from these choices: rock, paper, or scissors."
}

That is the code of the answer I came up with. Not sure if it's ideal, but, it seemed to work for me. I'm sure there are many other ways to do it also, just seemed simpler to do it this way to me. Good luck!


#3

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