What did I do wrong?


#1

So I have been working on this a long time and I can't figure out what I did wrong,

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

It says that that there is an unexpected token "else" but if I did take it out, wouldn't it be wrong?
This is the whole thing;

/*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!";
    }
console.log(compare("paper","paper"));
}


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

#2

I am confused, this is your function your compare:

var compare = function(choice1, choice2) { 
    if(choice1 === choice2) {
        return "The result is a tie!";
    }
console.log(compare("paper","paper"));
}

I don't understand why you call a console.log, and why you close your function. Your else if should be inside the compare function as well, and after is (that is why it says unexpected else, else if can only be after if)


#3

Okay sorry I fixed that problem... but now its says unexpected else again...

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

}


#4

I would recommend to indent your code, it seems you created the same problem:

else if(choice1 === "rock") 
{ if(choice2 === "scissors") 
{ return"rock wins"; }
else 
{ return"paper wins"; 
} 
} <-- closes else if, which is good
} <-- matching something i can't see, most likely closing your function

#5

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

It's the other one that doesn't .-. but arn't they the same?


#6

Well yes, if you make a mistake by closing the function to early, of course the later part of the code doesn't work. Can i see your full code after you organized it? I see very little indention, and no logic in when you open and close a curly bracket, making it really easy to make this kind of mistake


#7

Yeah of course just wait a little :3


#8

So,I don't really get what you mean when you say closing the function to early..can you explain that?


#9

Well, if you have a simple function with if and else statement:

var myFunction = function():
  if (condition){
    /* do something */
  } else {
    /* do something else */
  }
} <-

you close the function with a closing curly bracket (}), i marked it with a <-, but what i suspect you do is this:

var myFunction = function():
  if (condition){
    /* do something */
  } 
}
  else {
    /* do something else */
  }

Now you close your function before the else statement (or in your case else if), which can't be the case. else if should always be after if or another else if. By closing the function to early, you prevent this


#10

Okay I think I got it thank you :smile: