5/9, Compare() is created already?


#1

Right, so here is the code:

var compare = function(choice1, choice2) {
      if (choice === choice2) {
        return "The result is a tie!";
    }
};

I get the "Oops, try again. Did you create a function called compare?"

Why is this happening? I did create the function "compare" right?


#2

The problem is that you are using choice instead of choice1. But the error message isn't really helpful at this point.


#3

How could I miss that! Thanks bud :wink:


#4

I still used choice 1 and choice 2, but it still doesn't recognize my compare function. I get an error saying computerChoice is not defined?


#5

Could you post your code pls?


#7

my entire code

var userChoice = prompt ("do you choose rock, paper or scissors?")

var computerChoice = Math.random(0,3)

console.log (computerChoice)

if (computerChoice >= 1){
console.log("rock!")
} else if (computerchoice >= 2){
console.log("paper!")
}else{
console.log("scissors!")
}
var compare = function (choice1,choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
;}


#8

maybe your problem is that you deleted the comment tags /**/ at the beginning and end.

my whole 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);*/

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
}
};


#9
Math.random(0,3)

Math.random doesn't take arguement so 0,3 will just be ignored and it still goes from 0 to 1. If you want to change the range you might multiply it by 3 and then Math.floor() it (round down to the next integer value). Currently none of your cases is possible so the computer always ends up with scissors. Also you should assign the value to computerChoice and not just console.log them.