(Your compare function does not return 'The result is a tie!' when there is a tie.)


#1



Exercise 5


Your compare function does not return 'The result is a tie!' when there is a tie.


/*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";
    }
};


#2

Is this your full code?
If not, please share your full code here. It could be possible that you don't call your function.


#3

I changed the original post


#5

I see, the validating of the answer can be a bit rough sometimes.
You've forgotten the "!" in "The result is a tie!". If you add the exclamation mark it should work.


#6

This may help you!

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random()

if (computerChoice < 0.34) {
console.log ("rock");
}
else if (computerChoice >= 0.66) {
console.log ("scissors");
}
else {
console.log ("paper");
};

var compare = function(choice1, choice2) {
if (choice1 !== "rock" || choice1 !== "scissors" || choice1 !== "paper") {
prompt ("You have to write rock, paper or scissor to play this game!")
}
else 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 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
}
};

(compare (userChoice, computerChoice);

#7

Hmm, I don't know about that one though. The exclamation mark should be the only issue with his code.

In your code you never change the var computerChoice into rock, paper or scissors.
So in the compare function you're checking wether or not a random number between 1 and 0 is equal to the strings "rock", "paper" and "scissors"?


#8

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