8/9 I don't know whats wrong with my code!


#1


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

It says there is a syntax error:unexpected token else but I can't figure out where, please help :disappointed:

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


#2

You can run it in some other interpreter to get a more informative error message.

For example, if I run it locally with nodejs:

$ node programmingforlife12.js 
/tmp/js/programmingforlife12.js:30
         else if(choice1 === "scissors"){
         ^^^^
SyntaxError: Unexpected token else

#3

under your last else if () log there is an extra } bracket before else that shouldn't be there other then that looks good to me. I had some problems with this one and your post helped thank you!


#4

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

//second condition:

    if(choice1 === "paper"){
        if (choice2 === "rock"){
    return "paper wins";

}
else {
return "scissors wins";
}
//third condition:

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

}
}
compare(userChoice, computerChoice);

I modified your code to look like this. Please try to understand the changes made
Your else if statements are a bit odd and like tan said above, your missing some closing brackets.
Heres a tip:
When you're doing if else statements, ask youself, questions to make sense of the logical flow:
like for example:
if( x condition is true){
//execute the second like of code
}else if( x condition is not true){
// execute this like
}else{
//else do this
}
understanding that its just like basic grammer will help you make sense of it all.


#5

Thanks for helping guys! I appreciate it! :smiley:


#6

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