Why does this code not work properly?
I am learining javascript on codecademy and just learn about functions. I built the rock, paper and scissors game and tried to include the features given in the suggestion in the final lesson. But it does not work. When I enter the input of rock, paper or scissors, nothing happens. How to fix it? Is the 'return' preventing the game to run properly in the compare function?
var game = function () {
var userChoice = function (user) {
user = prompt("Do you choose rock, paper or scissors?");
if ((user != "rock") && (user != "paper") && (user != "scissors")) {
alert("The input is wrong.");
userChoice();
} else {
return user;
}
};
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins";
}
}
};
compare(userChoice(), computerChoice);
};
game();
This is the first time I am learning coding after learning HTML and CSS and I need some help.