Stuck in line 21

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/2/5?curriculum_id=506324b3a7dffd00020bf661

I am stuck at line 21 with an error saying " expected an identifier and instead saw ‘else’ . missing “;” before statment.

var userChoice = prompt(“Do you choose Rock , Paper or Scissors”);
if ( userChoice !== “Rock” && userChoice !==“Paper” && userChoice !==“Scissors” ) {
prompt(“Not a valid answer ! Choose another answer”);
}
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) {
Prompt(“It’s a tie ! Choose again”);
}};
else if ( Choice1 === “Rock”) {
//this line up
if ( Choice2 === “Paper”){
return “Paper wins”;
}
else if (Choice2 === “Scissors”) {
return “Rock wins”;
}

else if (Choice1 === "Paper") {
    if (Choice2 === "Rock") {
        return "Rock wins";
    }}
        else if (Choice2 === "Scissors") {
            return "Scissors win";
        }}

else if (Choice1 === “Scissors”) {
if (Choice2 === “Rock”) {
return “Rock wins”;}
else if (Choice2 === “Paper”) {
return “Scissors win”;
}}
compare (userChoice,computerChoice);

Take a look on line 20. You have a syntax error:

var compare = function(Choice1 , Choice2) {
	if (Choice1 === Choice2) {
		Prompt(“It’s a tie ! Choose again”);
	}};

The second curly brace closes the function. Yet, on the next line, you have the else if code block. That code block continues the if/else if/else decision blocks.

How do you fix it? Remove both the curly brace and semicolon like this:

var compare = function(Choice1 , Choice2) {
	if (Choice1 === Choice2) {
		Prompt(“It’s a tie ! Choose again”);
	}
	else if ( Choice1 === "Rock") {

and close off the function after the last decision code block, i.e. before the last line that runs the compare(userChoice, computerChoice);.

A tip: It’s very helpful to format your code into levels. Why? It helps readability. But it also helps you to quickly see the opening and closing curly braces. Remember, you need to match these up, as they hold the code within that code block.

I wanted to point out another syntax error. Take a look on line 19 at the prompt command. What do you see about the command? It’s capitalized. Whoops. Change it to a lowercase like this:

var compare = function(Choice1 , Choice2) {
	if (Choice1 === Choice2) {
		prompt(“It’s a tie ! Choose again”);
	}
	else if ( Choice1 === "Rock") {

Cheers.

Thx @hellofromtonya for helping … but I have edited my code and it does not have that error but there is a problem … when I run it , It tells me what the computerChoice is but it doesn’t decide who is the winner ( it doesn’t print e.g : “Rock wins” )

var userChoice = prompt(“Do you choose rock, paper, rope or scissors?”);
if (userChoice != “rock” && userChoice != “paper” && userChoice != “scissors” && userChoice != “rope”) {
prompt (“Not a valid answer, please choose: paper, rock, rope or scissors!”);
}

var computerChoice = Math.random();
   if (computerChoice < 0.20) {
computerChoice = "rock";
} 
else if(computerChoice <= 0.50) {
computerChoice = "rope";

}
else if(computerChoice <= 0.70) {
computerChoice = “paper”;
}
else {
computerChoice = “scissors”;
}

 console.log("Computer: " + computerChoice);
 var compare = function(Choice1 , Choice2) {
     if (Choice1 === Choice2) {
         prompt("It's a tie ! Choose again");}
         else if (Choice1 === "Rock") {
             if (Choice2 === "Paper") {
                 return "Paper wins";
             }}
             else if (Choice2 === "Scissors") {
                 return "Rock wins"; }
                 
        else if (Choice1 === "Paper") {
            if ( Choice2 === "Rock") {
                return "Rock wins"; }
                else if (Choice2 === "Scissors") {
                    return "Scissors win"; } }
        else if ( Choice1 === "Scissors") {
            if (Choice2 === "Rock") {
                return "Rock wins"; }
                else if (Choice2 === "Paper") {
                    return "Scissors win";
                }}};
                
                compare(userChoice , computerChoice)

Compare the case of the strings you’ve asked for in the userChoice prompt. You have a check right after that to make sure the user typed in one of the choices in all lowercase.

Now compare that to choices in the function.

This topic deviates so much from the actual lesson instructions it is treated as experimental and moved to the Corner Bar for free discussion.

Q&A topics are intentionally geared to and confined to actual lesson instructions and expectations. If you are having trouble passing the exercises, then the only thing to do is go back to the start and follow the instructions, explicitly and to the letter, including variable names, text literals, etc.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.