Help resolving unexpected token error paper, rock, scissors


#1


I'd appreciate any advice! I'm stuck on Lesson 6. What if choice1 is rock? https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/2?curriculum_id=506324b3a7dffd00020bf661#

I keep getting an unexpected token error else and cannot reconcile it! Any advice is appreciated!!

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 === "scissors"){
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors win";
    } else {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors win";
        };
    };
}


#2

Hi, here is your corrected code!

The issue was on your 18 and 24 lines: you forgot "}" to close your if/ else statement!

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 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        } else {
            return "scissors win";
        }
    } else {
        if (choice2 === "rock") {
            return "paper wins";
        } else {
            return "scissors win";
        };
    };
}

#3

Thank you so much! I didn't actually realize I needed to close the bracket there. Great learning opp :slight_smile: Thanks again!


#4

You're welcome! :slight_smile:

Here is also the useful information to help you clarify this issue: JavaScript If...Else Statements


#5

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