How to bring up prompt again if the answer is invalid?


#1

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

Hello, so I'm at the lesson 9 of the course, and I borrowed this piece of code from the forum to see if it would work, currently I'm working on a function to make my var userChoice equal to the string the user inputs on the prompt, and make it so if the imput is neither "rock", "paper" or "scissors", return a message asking the user to input a valid word, but whn I run it, if i write something random, the computer doesn't recognizes it, so I'm wondering, how can I make it work, and make it so that if the user inputs an invalid word, the promp comes up again to ask for an imput instead of just moving on with the rest of the code?
If any of you'd help me that'd be marvelous.


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

var user_choice = function (userChoice) {
if (userChoice==="Rock") {
    userChoice = "Rock";
    }
    else if (userChoice === "Paper"){
        userChoice = "Paper";
        }
        else if (userChoice === "Scissors"){
            userChoice = "Scissors";
            }
            else {
                return "Please select either rock, paper, or scissors to proceed"
            }
}
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 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    }
    else if (choice1 === "rock") {
        if (choice2 === "paper") {
            return "paper wins";
        } else {
            return "rock wins";
        }
    }
}

compare(userChoice, computerChoice);


#2

functions allow us to run a block of code multiply times

If we want to execute this block of code (the function), we need to call it. You never call the user_choice function, so it never executes


#4

Thank you! I never saw that


#5