6. SyntaxError


#1

Why it doesn't work? :confused:

    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";
    }
}
else if(choice1 === "scissors")
{
    if(choice2 === "rock")
    {
        return "rock wins";
    }
    else
    {
        return "scissors wins";
    }
}
}
};
compare(userChoice, computerChoice);

#2

hi,

you have posted code but not indicated what the error is?

When I had a look I noticed I had entered a line below the prompt - have you tried adding a second line directly beneath the prompt?

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

// Question to the user

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User:" + " " + userChoice);

I am not that far ahead of yourself so I am still learning to but see if it helps adding the additional line of code that shows up in the command window as everything else looks fine (i think)


#3

This part is delivered by codeacademy i think, so im guessing it should be correct.

And that's my error :

SyntaxError: Unexpected token else

#4

ah yes, I see you are right sorry.

Ok if the error message is saying unexpected token else then looking at the code perhaps the indentation is not quite right?

another learner a bit further ahead has their code indentations not as it should be and once they rectified that it worked for them.

Perhaps this is worth a shot with yours?

else if(choice1 === "rock")
{
if(choice2 === "scissors")
{
return "rock wins";
}
else // perhaps this needs aligning with the if statement to flow correctly?
{
return "paper wins";
}
}
else if(choice1 === "paper")

try aligning the code like so:

if (choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock") {
if(choice2 === "scissors"{
return "rock wins"
}
else{
return "paper wins"
}
}

};

// sorry when I pasted the code it did not copy in correctly - just move your else back in line with the return "rock wins"


#5

here is the snapshot of the code which I submitted for that part and, from what I can see it is almost the same as yours.

The only difference I noticed between both is just that misalignment with part of the code of your's where the else was not aligned the same as the return:

Hope it helps you resolve your error


#6

First - compiler, syntax or whatever it's name is, doesn't care about indentations, We use it for clearness and readability of the code.
Second - My mistake was.. additional { } brackets after if(choice1 === choice2). But still I don't get what it has to do with 'else'.
But it works now. Thanks for your help.


#7

I didn't see too many { } brackets in the code you posted and sorry I.didn't really help but glad you managed to debug and thanks for clarifying about the clearness and readability that is gud to know that...