I don't understand why I need this specific semi colon to run my code


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


My code works just fine first of all. On line 66 else(choice1 !== "rock","paper","scissors","rope");{ the semi colon that comes right before the curly brackets I wrote accidentally but when I re read my code I thought the semi colon was unnecessary. Once I took it out I got this error:SyntaxError: missing before statement. Once I put back the semi colon my code works just fine again. I don't have a semi colon before curly brackets anywhere else so I'm really confused as to why I need to have this one. Could anyone please explain it to me?


var playGame = function(){
var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
    computerChoice = "rock";
} 
else if(computerChoice <= 0.50) {
    computerChoice = "paper";
} 
else if(computerChoice <= 0.75){
    computerChoice = "scissors";
} 
else{
    computerChoice = "rope";
    }
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 if(choice2 === "paper"){
            return("paper wins");
        }
        else{
            return("rock wins");
        }
    }
    else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return("paper wins");
        }
        else if(choice2 === "scissors"){
            return("scissors wins");
        }
        else{
            return("paper wins");
        }
    }
    else if(choice1 === "scissors"){
        if(choice2 === "rock"){
            return("rock wins");
        }
        else if(choice2 === "paper"){
            return("scissors wins");
        }
        else{
            return("rope wins");
        }
    }
    else if(choice1 === "rope"){
        if(choice2 === "rock"){
            return("rock wins");
            }
        else if(choice2 === "paper"){
            return("paper wins");
            }
        else{
            return("rope wins");
            }
    }
    else(choice1 !== "rock","paper","scissors","rope");{
        return("Not a valid option!")
    }
}
var result = compare(userChoice, computerChoice)
if(result === "The result is a tie!"){
    playGame();
}
else if(result === "Not a valid option!"){
    playGame();
    }
else{
    console.log(result);
}
};
playGame();


#2

Hello :slight_smile: Well, you are right that semicolons before curly brackets are simply evil.

Problem with your code is not in the semicolon. You should not specify the condition of else statement. else means in any other case, without any conditions. This is why you are getting a syntax error.

And the condition is also wrong, JavaScript does not support chained comparisons so you have to use multiple !== operators or use array.IndexOf method.

Why does it work with the semicolon? The semicolon terminates the else statement and it's never really executed.


To solve this problem you can delete the condition of else. return terminates the function so if choice1 is "rock", "paper", or "scissors" function will be terminated before executing this part of code.

Or you can delete the whole else statement and leave only return("Not a valid option!"), reason is the same.


#3

I had a look at your code and well done. Upon removing the semi-colon, which the placement of which makes zero sense as to why it runs, if you make the below code:

else(choice1 !== "rock","paper","scissors","rope");{

Like this:

else(choice1 !== "rock","paper","scissors","rope")
{

It solves the issue and will run.
It's more than likely the fault of the interpreter on the website than the code itself. Plus Codecademy teaches very bad practice of readability of Javascript. Changing the code to my own preference seems to have solved it.


#4

■■■■ I knew it couldn't be that easy. I didn't really think that one through and thought that I would try to put a couple of strings in there to see if it would work and I thought it did. Thank you very much for explaining it to me, appreciate it.


#5

I just use Codecademy to learn the basics. I've always wanted to code but I've never knew where to start or how to start. I've heard that it's bad practice and teaching bad habits but I just wanted to learn how things work if that makes sense. I'm hoping that I will learn much more and detailed information from my school but for now this is nice.
Thank you both for your feedback though. Much appreciated.


#6

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