"Rock, Paper, Scissors" Why is my code only printing out the computers response and not the outcome of the game?


#1

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 (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==="rock"){
return "rock wins";
}
else{
return "scissors wins";
}
}
};


#2

Have you added any console.log statements with userChoice in parentheses?


#3

where would i insert the console.log? would it be where i have the return functions?


#4

You could use console.log to print out the compare function and its parameters. Just add a line after your code.


#5

Sorry I am a complete beginner so I'm not familiar with all of the functions yet.
like this?: console.log(compare(choice1,choice2));


#6

Exactly like that! You can also add something like console.log("The user has selected: " + userChoice) before the line where you start the compare function.


#7

how do i get it to where it would print the return functions i wrote in the code?
i.e. "scissors wins"
I really appreciate your help tho


#8

Always happy to help, helps me remember past lessons.

When you wrote your compare function, you used choice1 and choice2 as your parameters. When you call the compare function, now you can use the userChoice and computerChoice variables that you wrote the code for:

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