What if choice is scissors


#1

Hey guys,

not sure what is wrong with my code but it keeps saying syntax error, keyword else

code is listed below. if anyone could help that would be greatly appreciated

Thanks in advance

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


If scissors is picked. anyone see whats wrong?
#2

else comes after if, so check if yours do. Fixing your indentation helps spot these things and avoid them appearing in the first place


#3

they are all good, i can't see whats wrong


#4

They're not


#5

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";
}
}
}
console.log (userChoice, computerChoice)
var compare = function (userChoice, computerChoice)


#6

What makes you say it's wrong? If you think something should behave differently then that probably says a lot about where you need to be changing things


#7

every time I try to fun it, it says syntax error: unexpected keyword "else"

I don't know where the error is, I am asking if anyone sees where the error is.


#8

run it* not fun it. my bad


#9

So.. What you're saying is that you created two threads of the same issue and that in the other one (now merged into the same thread) I've both said how to find the problem and and drawn a big red arrow pointing at it.

I'm at a loss for what else you could want.


#10

honestly, i just don't understand, are you saying i should delete the {?


#11

No, you will need to compare what you mean your code to say to what it says and eliminate differences.

Right now you have:

var blahblah  = function() {
}
else {
}

And that's not valid syntax. else comes after if. Like so:

if (condition) {
}
else {
}

So do you mean to say "create a function, otherwise do this other thing" -- that doesn't make sense, else is what you do when a condition was not met, it can only come after saying "do x if y, else do z". Refer to what you meant, and write that instead.

There's no "delete } to solve your problems", the syntax does not decide what you want. You have to refer to what you want, and then write the corresponding code.

And if you're not sure what you want your program to do then you'll have to figure that out before thinking about any of the code. That's just a matter of lining up events, you'd be looking at the rules for the game to figure that out.


#12

ok, thanks for your help. I was understanding it step by step as the process went on. now its all muddied. Thanks a lot for your help. I will go back to the beginning and try to get the idea for the code, to understand what i want the code to do.


#13

"I understood what was happening but now it's all mud."

That's some poetry-level stuff right there.


#14

lol. not much for poetry either don't worry.

its the else/if and the if statements that trip me up.


#15

It's just

if (CONDITION) {
    THEN DO THIS HERE STUFF
}

optionally followed by

else {
    DO THIS OTHER STUFF
}

You could read this (far better written than codecademy's instructions)


#16

Hey the help is much appreciated, I feel like I'm bothering you and I hate that feeling.


#17

Posting two threads for the same thing that's bothering me, but other than that, not at all. Go read that mozilla article!


#18

reading it. so just wondering then. in the instructions it noted to do an else if clause for the second (if the first choice was paper) step and that ran fine, but the same didn't happen for the last step with scissors


#19

Then you probably just didn't do the same thing.


#20

I will look over it again, I am really sorry for all this. I really do appreciate all your help.

and as for the poetry, I just finished writing my thesis so still got unused words in my head