Help, syntax error else, WIP


#1



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


syntax error, else


Replace this line with your 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 {
      (choice2 === "scissors")
      return "rock wins"
  }
  
  else {
      "paper wins"
  }
     } 
};


#2

I am still working on it, but I need some help...


#3

Hi this part

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

should have that structure

    else if (condition) {
        if (condition) {
            #do somthing
        }
        else {
            #do something
       }
    }
}#this one close the functon compare

#4

Thanks, but I'm a bit confused. I am new to coding/scripting and I am still getting the hang of things.


#5

so you got that

else if (condition) {
        if (condition) {
            #do somthing
        }
        else {
            #do something
       }
    }
}#this one close the functon compare

the instruction says..

else if statement where the condition is choice1 === "rock"

else if (choice1 === "rock") {
    .......
}

and

Inside this else if statement, write an if / else statement. If choice2 === "scissors" , return "rock wins"

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

then

Else, return "paper wins".

else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return "rock wins"
    }
    else {
        return "paper wins".
    }
}
}#that close the compare function

Hope that could help you


#6

ok, heres what I got (sorry about this :sweat_smile: .)
Getting a syntax error: unexpected token, else

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


#7

this line

if choice2 === "scissors" {

you should put choice2 === "scissors" inside () then you didn't closed your first if statement

if (choice1 === choice2) {
    return "The result is a tie!";
}#it's missing the }

#8

ok.... I'm working through this...
still getting syntax error, else

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


#9

Now here

if (choice1 === choice2) {#it's missing the { 
    return "The result is a tie!";
}

#10

YEA! It worked! Thanks so much!


#11

I think I have a problem..... again....
Syntax error, else >_< I am NOT gonna help anyone debug their codes until I get better...

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


#12

maybe if you look at your code like that

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

Can you see now what's wrong? Remenber that you should also close the compare function


#13

The only differences I am noticing is the positioning of the }'s and words. Am I missing something?


#14

it's not missing anyhthing but you have have more } than you need at the end


#15

hows this?

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

}
};


#16

Can you post all your code?


#17

here you go...

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

#18

in this case it's missing the closing } of the compare function

var compare  = function(choice1, choice2) {#open compare function

#here put all the if , else if statement

};#close the compare function

#19

there are so many missing closing curly brackets in this code, lets start with:

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

there are already two missing curly brackets here, one to close your if choice2 is scissors, and one to close your else.

@wizmarco is right, the closing curly bracket at the end is missing, there should be 3:
one for the else if statement, one for the else, and one for your function, you have two closing curly brackets


#20

man... Ive been coding all moring. I'm gonna take a break, here's what I hope I fixed... :sleeping:

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