4/8 Error: Make sure to call your compare function with userChoice and computerChoice as the arguments!


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

compare("userChoice", "computerChoice");

Can someone find the error in my code? It tells me to call it, but I thought that's what I'd done at the bottom.


#2

@mickmorrell,
If you use
"userChoice" you are using a string
and not the created variable userChoice

So change the arguments in your call of the =compare= function
like

compare(userChoice,computerChoice);

but as the =compare= function is using the return statement
the compare function will return a RESULT with NoDisplay

Therefor use

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

#3

Sorry for the very late reply, but thank you very much!


#4

This helped me too, thanks!


#5

This topic was automatically closed after 24 hours. New replies are no longer allowed.