8. What if choice1 is scissors? SyntaxError: Unexpected token else


#1


I have edited and reedited a handful of times. What am i missing folks?
Oops, try again. There was a problem with your syntax.

SyntaxError: Unexpected token else


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 === "paper")  { 

        if ( choice2 === "rock") {
        return "paper wins";
        
       } else {
        return "scissors wins";
    

} else if ( choice1 === "rock") {
    if ( choice2 === "paper") {
        return "paper wins";
        
    }else{
        return "rock wins";

} else if ( choice1 = "scissors") {
    if ( choice2= "rock") {
        return "rock wins";
    
    }else{
        return"scissors wins";
    }
}
};

compare(userChoice,computerChoice)


#2

@tonysgarcia,
You are missing 1 closing-curly-bracket-}
in the ELSE-IF { if {} else {} }

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#3

I read your forum post about functions with two paramaters. I especially liked where you defined an argument and variable and gave us the colloquial descriptors.

I added one more code block and athird curly brace to the new end of my code.

I still receive this error:

Oops, try again. There was a problem with your syntax

SyntaxError: Unexpected token else

`
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 === "paper")  { 

    if ( choice2 === "rock") {
    return "paper wins";
    
   } else {
    return "scissors wins";

} else if ( choice1 === "rock") {
if ( choice2 === "paper") {
return "paper wins";

}else{
    return "rock wins";

} else if ( choice1 = "scissors") {
if ( choice2= "rock") {
return "rock wins";

}else{
    return"scissors wins";

}else if ( choice1 === "paper") {
    if ( choice2 === "rock" ;
    return "paper wins";

}else{
    return "scissors wins";
}

}
};

compare(userChoice,computerChoice);
`

I don't know why the formatting will not work.


#4

@tonysgarcia

var compare = function(choice1,choice2) {
//-------------------------------------------
if (choice1 === choice2)  {
    return "The result is a tie!";
} 
//-------------------------------------------
else if  ( choice1 === "paper")  { 
        if ( choice2 === "rock") {
             return "paper wins";
        } 
        else {
             return "scissors wins";
        }
} 
//-------------------------------------------
else if ( choice1 === "rock") {
       if ( choice2 === "paper") {
           return "paper wins";
       }
       else{
           return "rock wins";
       } 
}
//-------------------------------------------
else if ( choice1 = "scissors") {
       if ( choice2= "rock") {
            return "rock wins";
       }
       else{
            return"scissors wins";
       }
}
//-------------------------------------------
//End of function
};

========================================

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#5

I always fence my code blocks. I will try again.


#6

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 === "paper")  { 

        if ( choice2 === "rock") {
        return "paper wins";
        
       } else {
        return "scissors wins";
    

} else if ( choice1 === "rock") {
    if ( choice2 === "paper") {
        return "paper wins";
        
    }else{
        return "rock wins";

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

compare(userChoice,computerChoice);

I fixed it. I did not put a line of space between my code and my closing backticks.


#7

^ Have a look at this line, you are missing closing parenthesis and opening curly bracket


#8

Fixed the problem. Same error message. Here is my updated 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!";
    
    } else if  ( choice1 === "paper")  { 

        if ( choice2 === "rock") {
        return "paper wins";
        
       } else {
        return "scissors wins";
    

} else if ( choice1 === "rock") {
    if ( choice2 === "paper") {
        return "paper wins";
        
    }else{
        return "rock wins";

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

compare(userChoice,computerChoice);

#9

I just had a closer look at your code there's a few mistakes, few closing brackets missing and of your statements are incorrect

As you said

I would suggest reset lesson read instructions and start again..

Keep in mind the syntax, as Leon showed here


#10

Man, I thought I had this one down. Eh, back to the drawing board.


#11

Actually you will find it much easier, starting from scratch is easier than working with messy code,

Previous lessons code will still be there, you just need to do current lesson..

Have a drink of water and then start :green_heart:


#12

Budget snack. Cup of noodles.


#13

Okay you go ahead do that, I'm sure you pass but if you need any help then paste in..

Make sure to close your brackets when opening it then go inside and type, it's a good habit and in that way no brackets will be left open


#14

@tonysgarcia you're almost there, buddy