" What if choice1 is scissors? " (compare function propably not right)


#1

Hello friends, i am trully need your help to close my game. I think i ll doing my best and i have been tryed for few hours with any help, but finally i have problem with final step..

here is my code:

START CODE
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);
compare(userChoice,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 "paper wins";
}
}
};

END CODE

Best Regards,
G.D.


#2

This line of code should come after your compare function.


#3

many thanks i solved it, i have problem with below...after few hours when your are amateur (cause i am not programmer but Marketeer), eyes loses concentration

START CODE
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "paper wins";
END CODE


#4

Ah ok, when choice1 is scissors and choice2 is rock, rock wins as you have coded. When choice2 is paper and choice1 is scissors, scissors should win.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.