Having trouble getting the result to return inside a function for my basic rock paper scissors game.
I've tried everything, including console logging the compare and putting it inside a variable.
Strange enough, console.log(compare) is returning undefined.
If anyone can assist, I would greatly appreciate it.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
} 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 wins!";
}
} else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}