Step 8 - What's wrong? It's driving me crazy!


#1

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);

//function

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!";    
    }
}

else(choice1 === "scissors") {
    
    if(choice2 === "rock") {
        return "Rock wins!";    
    }
    else {
        return "Scissors wins!";    
    }    
}

};

compare(userChoice,computerChoice);


#2

At first glance it seems to be ok. Maybe some output messages could be slightly changed but the structure seems to be fine. What is your error message?


#3

The error message is this:

SyntaxError: Unexpected token '{'. Parse error.


#4

else doesn't have and doesn't need a condition.


#5

Wow! The program works now! I can't believe I didn't notice that. Thanks for the help :smiley:


#6

So what do you change it to exactly kind sir?


#7

use else if instead of else if there is a condition following. But is your code the same?


#8

I just removed the (choice1 === "scissors") from the else statement. I hadn't noticed it while inspecting the program's bugs. Glad it works now though.