Discuss Scratch

turkey3_test
Scratcher
1000+ posts

Learning JavaScript: could use some support and help

Prior to JavaScript, I only knew a little CSS, HTML, Java, and Python, but hardly much at all. I decided to learn JavaScript, and am doing so on Codecadamy. I am about 28% finished with the course but am stuck on a Rock Paper Scissors game part. Here is my code:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(); //random value between 0 and 1
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
}
var compare = function(choice1, choice2)
{
    if (choice1 === choice2);
    {
         return "The result is a tie!";
    }
    if (choice1 === "rock");
    {
        if (choice2 === "scissors");
        {
            return "rock wins";
        } 
        else; 
        {
            return "paper wins";
        }
    }
}
The problem is the “else” near the bottom. When I run the code, I get SyntaxError: Unexpected token ‘else’. Can you help me debug this? Thanks!

danwoodski
Scratcher
100+ posts

Learning JavaScript: could use some support and help

Try getting rid of the semi-colons after the if/else statements (instead of else; put just else)
turkey3_test
Scratcher
1000+ posts

Learning JavaScript: could use some support and help

danwoodski wrote:

Try getting rid of the semi-colons after the if/else statements (instead of else; put just else)
I did and the same thing happens.

turkey3_test
Scratcher
1000+ posts

Learning JavaScript: could use some support and help

turkey3_test wrote:

danwoodski wrote:

Try getting rid of the semi-colons after the if/else statements (instead of else; put just else)
I did and the same thing happens.
Never mind, it worked. Thanks so much!

veggieman001
Scratcher
1000+ posts

Learning JavaScript: could use some support and help

Conceptually, just remember that if and else aren't lines themselves; they require what's enclosed in the brackets (which is their argument). If anywhere, it would make the most sense to but a semicolon after the curly brackets.

Powered by DjangoBB