Rock, paper, scissors (Part 7) Syntax Error. Help needed


#1

Hey can someone help. I have no clue what or why my code here isn't working. :confused:

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


#2

you missing a curly opening bracket do you see it ?

if(something) {
// do something
}

#3

hey man, tried that but for some reason is still not working??


#4

Paste I your full updated Javascript code


#5

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

Appreciate your help


#6

missing semi colons on both statements


#7

@bradleykeleghar,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )

#8

hey have added the semi colons and have also read the attached article which I understand however it still persists with syntax error. Really frustrating lol


#9

@bradleykeleghar,
Well, present the code you are using....


#10

the following code worked for me. the only difference I see between yours and mine is that I don't have a semi colon after "paper wins", and I have an extra set of curly braces at the very end

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


#11

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


#12

the last line I have removed the final curly bracket


#13

@bradleykeleghar,
Do not use a semi-colon-; in your IF ELSE-IF statement.
For the Javascript Interpreter the semi-colon-; is an End-of-Statement indicator,
and it will disturb th flow-control......

Have an other look at the build of the compare function
http://discuss.codecademy.com/t/rock-paper-scissors-part-7-syntax-error-help-needed/28617/7?u=leonhard.wettengmx.n


#14

I'm also struggling with this one dude right now but from the looks of it you need to put it inside the compare function where as yours is outside.


#15

what you mean the semi-colons? Why is that?


#16

@bradleykeleghar,
Have a read
http://stackoverflow.com/questions/25101936/why-does-adding-a-semicolon-sometimes-break-an-if-statement


#17

This is my working code. I did some mods to make reading it more logical, and also print the result in the console.

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("You: " + userChoice);
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 (userChoice === computerChoice) {
return console.log("The result is a tie!");
} else if (userChoice === "rock") {
if (computerChoice === "scissors") {
return console.log("Rock wins!");
} else {
return console.log("Paper wins!");
}
} else if (userChoice === "paper") {
if (computerChoice === "rock") {
return console.log("Paper wins!");
} else {
return console.log("Scissors wins!");
}
} else if (userChoice === "scissors") {
if (computerChoice === "paper") {
return console.log("Scissors wins!");
} else {
return console.log("Rock wins!");

}}}

#18

kleinowskit this is really helpful thankyou! I noticed that weirdly when I spaced some of the brackets it allowed the function to run after having removed the semi colons like Leonhard suggested. Your code seems to work the best.


#19

var userChoice= prompt("Do you choose rock, paper or scissors?");
var computerChoice=Math.random();
if(computerChoice<0.34)
{
computerChoice="rock";
}
else(computerChoice<=0.64)
{
computerChoice="paper";
}
else{
computerChoice="scissors";
}

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

not executing.please help.very confusing.


#20

@fussypanda,
Have a close look at the usage of the curly-bracket-{ }
comparing it to the IF ELSE-IF creation mentioned in
http://discuss.codecademy.com/t/rock-paper-scissors-part-7-syntax-error-help-needed/28617/7?u=leonhard.wettengmx.n