7. syntax


#1


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


getting a "SyntaxError: Unexpected token else" error. no idea


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


#2

try deleting the else statements.
only the
}else{

}


#3

This worked. Just finished Python so I am used to stating an "else" statement. Thank you


#4

no problem, I'm glad I could help. weirdly enough I think you are the third person I helped with that same problem this week. its pretty common so don't worry. good luck with the course :smile:


#5

Hi! I am having the same problem, what do you mean deleting the else statements, I though we needed these.. My code looks like this so far..

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

}
}
}


#6

delete the "else" line and just keep "return "scissors wins"; ,for example

basically if choice2 isnt equal to rock, we know to return that "scissors wins". the "else" statement becomes redundant.

Make sense?


#7

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