Issue locating problem! Assistance appreciated!


#1


Hi Guys. So, I'm VERY new to coding and have been trying to add to the Rock Paper Scissors JavaScript exercise to include Lizard and Spock (from The Big Bang Theory TV show) and I keep getting this ■■■■ 'SyntaxError: Unexpected token else' message and for the life of me I cannot find the syntax problem. Could someone please cast their eye over my code and help out. I've spent an hour staring at the screen but to no avail! :cry:


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


SyntaxError: Unexpected token else


var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("Player 1 has chosen" + " " + userChoice);

var computerChoice = Math.random();
if (computerChoice < 0.2) {
	computerChoice = "rock";
    } 
    else if (computerChoice > 0.2 < 0.4) {
	computerChoice = "paper";
    } 
    else if (computerChoice > 0.4 < 0.6) {
    computerChoice = "scissors";
    }
    else if (computerChoice > 0.6 < 0.8) {
        computerChoice = "lizard";
    }
    else {
        computerChoice = "spock";
    }
    
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 === "spock") {
            return "spock wins";
        }
        else if (choice2 === "lizard") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else if (choice2 === "spock") {
            return "paper wins";
        }
        else if (choice2 === "lizard") {
            return "lizard wins";
        }
        else {
            return "scissors wins";
        }
    }
    
    else if (choice1 === "scissors") {
        if (choice2 === "paper"); {
            return "scissors wins";
        }
        else if (choice2 === "rock") {
            return "rock wins";
        }
        else if (choice2 === "lizard") {
            return "scissors wins";
        }
        else {
            return "spock wins";
        }
    }
    
    else if (choice1 === "lizard") {
        if (choice2 === "spock") {
            return "lizard poisons spock";
        }
        else if (choice2 === "scissors") {
            return "scissors decapitates lizard";
        }
        else if (choice2 === "paper") {
            return "lizard eats paper";
        }
        else {
            return "rock crushes lizard";
        }
    }

    else {
        return "Error! Please only user the words 'rock', 'paper', 'lizard', 'spock' or 'scissors'";
    }
};

console.log(compare(userChoice, computerChoice));


#2

line 61:

if (choice2 === "paper"); {
            return "scissors wins";
        }

there is a unwanted ; after the if condition. Also, you can do this:

else if (computerChoice > 0.2 < 0.4)

you would a && operator, this one hasn't been taught yet, but you can simple do:

else if (computerChoice < 0.4)

if the number is smaller then 0.2, if will run, once if runs, else if will not run, change this for all your computer choices


#3

Hey!

Thanks for the reply - it's much appreciated. I corrected it just before you got back to me using an online 'lint' tool (whatever that is - http://www.javascriptlint.com/).

Can't believe a simple ';' was wrecking it all - lol. Definitely requires attention to detail this coding gig.

:smiley: :smiley: Really enjoying it though

Anthony


#4

As for the && operator - what's the point of this? Is this to stop the computer from running the code following? Is it done for performance or to stop unintended outcomes?


#5

Yea, there are plenty of tools to find the problems. Good to hear you resolved the issue

No, if you use if and else if and else, and if is true, the rest of the code won't run. && is just the and operator, if you want both conditions to be true:

if (conditiion1 && condition2)

only if condition1 and condition2 are true, if will execute. If either one or both is false, neither will execute. So, for example this line:

else if (computerChoice > 0.2 < 0.4)

is just wrong, you need the and operator:

else if (computerChoice > 0.2 &&  computerChoice < 0.4)

but i would just do what i demonstrated:

else if (computerChoice < 0.4)