SyntaxError: Unexpected token (


#1

Hello, I check and I check again and I don't know what the problem with '('
Could you help me please ?

(I don't succeed to color the code with ''' )

 var computerChoice()
    {
    var Choice = Math.random();
    if (Choice < 0.34){
	Choice = "rock";
    } 
    else if(Choice <= 0.67){
	Choice = "paper";
    } 
    else{
    Choice = "scissors";
    } 
    return Choice
};


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

var playAgain = function()
{
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = computerChoice();
    console.log("Computer: " + computerChoice);
    compare(userChoice, computerChoice);
};

playAgain()

#2

i did it man i can give the codes if u want there is no problem on it :smile:


#3

Hi michaelseyne, what is the error that you receive?


#4

hi :smile:

I wrote it in the title, it say : SyntaxError: Unexpected token (

I don't know why, the syntax seem ok to my eye

@berkayh : What do you mean you tried ? My code work with you ?


#5

Hi michaelseyne,

Sorry as i did not notice the title.

Anyway, i believe it's got to do with these line of code:

  1. var computerChoice() - should be function computerChoice()

  2. return choice should have a semi-colon, as in return Choice;

  3. all returns should end with a semi-colon

  4. print out the result after comparing

Should be good to go once the above changes are made. :wink:


#6

Well I see two problems in your code. The first is that computerChoice is not a function:

var computerChoice()

pretty close but to make it work you need var computerChoice = function(){...};
The other thing has to do with shadowing and is a bit more complicated. I know you had shadowing in an exercise on function where it wasn't named like this. But any way, maybe you remember that exercise where you just had to add a var to change the result? That is something similar here.
By using var computerChoice inside the playAgain function you create a new variable computerChoice that has nothing to do with the variable outside of playAgain that contains your function. So to avoid ambiuity only one of them can be accessed inside of playAgain and this is the most local one, the one that you just created. Which leads to the problem that this one is not a function therefor it will complain if you try to call a yet undefined variable. So maybe name one of them differently.


#7

Thank you shaunlau! It is fascinating to look many times the code and be sure there is no mistakes, and then finally, someone show you there is a lot :smile:


#8

Thank you very much haxor, I fixed it :smile: