What is my error?! 7/9


#1

I'm so confused right now. I've tried everything I know to do, and yet still I keep getting the same error message: "Unexpected keyword 'else'." Does anyone see what's wrong with this 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 === "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"
        };
    };
};

#2

Don't use semicolons after conditions:
See this for a longer explanation:
https://www.codecademy.com/forum_questions/52373a75548c3515940000dc#answer-529fa36052f863bb58000338


#3

So it should look more like this, right:

/*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 === "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"
        }
    }
};

#4

Probably yes or do you get any errors?


#5

I cant figure this one out either - i've tried everything and now i'm totally confused.


#6

can anyone post the correct code so i could study how to do it right? thx


#7

Me too...I have the same problem.


#8

Wait! Maybe try to put a semicolon where I put a smile.

/*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 === "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" :smile:
}
}
};


#9

Yep. No errors. I finally am starting to see my common errors in javascript. (But functions still befuddle me!)


#10

The smile is definitely a good place for a semicolon but it's not the kind of unplaced semicolon that would result in an error.


#11

I hear you. But I figured it out eventually! Now look at me, making while loops to slay a dragon. Keep going! Don't give up! As confusing as JavaScript can be, it's not impossible.