HELP! 9 Trying to repeat!


#1

i know it kind of defeats the purpose of me experimenting but i want to get my code to keep repeating itself until userChoice matchs one of the three options.
So i got an idea with how to do my experiment. can anyone help me or tell me what i'm doing wrong?

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);
// expermiental code
// trying to get code to repeat userChoice code if input doesn't match the three options.
if (userChoice !=="rock","scissors","paper") {
    for(var i=0;i<=userChoice.length; i++) {
        console.log(userChoice[i]);
    };

function compare(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"
        };
    } else {
        console.log("You didn't make a correct choice")
    };
};

console.log(compare(userChoice,computerChoice));

#2

Hey @alexmisanchez! You forgot a bracket at the end! Just add a bracket at the end and you should be fine! :smile: Hope that helped!


#3

without the extra if statement in the middle of my code. it works perfectly, what i'm trying to do is get my code to repeat unless i get a specified set of answers.

This above code ^^ is my attempt at reaching that goal. currently it still does not work. (with the bracket) all it does is print out what i put in userChoice.


#4

if you want to repeat the whole code you could the do-while loop. Here is a documentation.