8/9 syntax error:unxpected token else


#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 win!";
}
}
};

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

#2

Can anyone please help what is wrong with my code?


#3

Hey @boardninja65643,

The problems start here:

That whole part has some problems.

Let's start here:

You missed some brackets and an else statement in there.

Should be something like:

else if (choice1 === "rock"){
    if (choice2 === "scissors"){
        return "rock wins!";
    }else{
        return "paper wins!";
    }
}
    else if(choice1 === "paper"){
        if(choice2 === "rock"){
        return"paper win!";
}
else{
return "_____";
         }
         }

The parts that are on the left side unlike others show the edited parts.

You see those parts? They show what is wrong with the code. :smile:

-----------------------------------------------------------------

Next part:

To use the compare function on a console.log, you need to use a variable, for example result.


var result = compare(______,______) console.log(result);

Get what I did?:smile:

Well, thats what you're supposed to do.

But don't try to copy it. It's called copyproof. :sunglasses:


#4

Ahh Just Right before me! Oh, Speedy @ragezapper!


#5

Er uh thanks? :confused:


#6

Thanks! 😃So.. I just create a variable named result equal to compare a & b and then print out with console.log
Example:
Var result = compare(choice1, choice2);
Console.log(result);


#7

Yeah, something like that. Remember to put var as lowercase. :smile:

Just remember that choice1 is userChoice and choice2 is computerChoice. :smiley:


#8

Sweet! Okay thanks again ragezapper that is so cool that you can make a variable of a function that is already created😎


#9
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!"
}
};
var result = compare(userChoice,computerChoice)
console.log(result);

It keeps giving me the same error what can be wrong with this code please help me I been wanting to move to the next step.


#10

You accidentally added another bracket here:

And you need an extra one here:

That would work :smiley: