What if choice1 is scissors? Stuck! :-/


#1

Hey guys / gals,

Im on day 2 of my JavaScript binge and i seem to be stuck. Don't know whether its due to lack of sleep or not enough coffee lol

I thought I had my if / else / if statements down but apparently not. Im on section 8 of the rock paper scissors build.

Thank you in advance to anyone who is kind enough to help :slight_smile:

Regards
Roy

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

#2

Sorry, forgot to include my error.

SyntaxError: Unexpected token {


#3

Take a look at the end of your return statement.. You're missing a quotation mark. :slight_smile:


#4

Hey Kylea

Thank you very much for you assistance , hawk eye and prompt response.

Defo a lack of both sleep and coffee on my part! lol

I corrected the missing " and still get the same error.

Thank you in advance for any help.

Corrected code -

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

compare (userChoice, computerChoice)


#5

:slight_smile: No problem, I've heard that programmers are people who can convert caffeine into code.

anyways, I'm sorry you're having this error! I'll go ahead and let you know that this error typically means that you didn't close a bracket, so I would advise you to look for that. I will examine your code shortly, but I'm in the middle of something right now. I'll dive in within the hour. :slight_smile:


#6

That's definitely an accurate statement (excuse the terrible pun lol)

No worries, you have helped me plenty already. Iv topped up on caffeine so hopefully the conversion comes to me before you are done.

Anyway, thank you for your help and ill let you know how I get on :slight_smile:


#7

Well I have read your code in full, and you're only missing one closing brace. It's the one to your last else statement.
In other news, that's not the solution because the same error message is proceeding. I'm continuing to look, but just letting you know that it's not the braces. :slight_smile:


#8

Thanks for that. I literally found the missing brace as you replied and further to that, my last else / if statement was missing the if.

All sorted now thankfully! :slight_smile:

Again, thank you for your help. Im sure this will not be the first time i get stuck lol


#9

That's so funny because I just found that you were missing the if in your else if statement. You can't have a condition for an else statement, so yeah, that's what the problem was. Though you did this all on your own, it definitely helps to talk to somebody about it, so I'm glad you asked for help! Great job. :slight_smile:


#10

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