7.What if choice1 is paper? Stuck need help


#1


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


#2

When you add more structure to your syntax, you can quickly recognize your own mistakes

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"
		}; // semicolon not needed 
	}
} // Here you close the function?
else if(choice1 ==="paper") 
{
	if(choice2 ==="rock") 
	{
		return "paper wins";
    }else
	{
		return"scissors wins";
    }; // semicolon not needed 
};

#3

closing curly brackets (}) of if, else if and else shouldn't have semi-colons

checking if choice1 is paper should be part of your compare function:

else if(choice1 === "rock") {
     if(choice2 === "scissors"){
     return "rock wins";
     }else{
         return "paper wins"
     };
    }
}  // this curly bracket closes the function, a bit too early

#4

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

still not working for some reason


#5

now you don't close the function at all?


#7

Hello guys !

I've read all the messages about this subject but I still can't underqtand. Here is my block :

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

};

The site tells me "unexpected end of input" but I cant understand what does thatt mean and how to correct it :confused:

Thank you !


#8

you didn't close your function


#9

well how shall I do this ? I'm a bit lost with all these curly brackets


#10

add a } to close your function? You have to keep good track of which curly bracket belongs to what (good indention and comments can help)


#11

Okey I'll try thanks a lot ! :smiley:


#12

Okey I'm back with a new problem :confused:

The thing is that I don't always understand what should I correct in terms of what the site says. Here is my code, I need to finish my function :

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 === "rock") {
    return "rock wins";
}
else {
    return "scissors wins";
}
};
}

console.log(compare(userChoice, computerChoice))

Sorry for my level


#13

now your function ends to early, where do you think your function ends?

closing curly brackets of if, else if and else (}) shouldn't have semi-colons


#14

oh okey I think i get it do I need to put the semi-colons at the last curly bracket ?
instead of the one juste before ?


#15

if you want you can put ; at the } which closes the function, yes, but not on the other ones


#16

well I tried different positions, and I still can't understand what defines the end of my function and what definies the end of else if statement. I'm so sorry to be that bad :confused:


#17

well, you build a function:

var example= function(){
  
}

inside your function, you make 3 else if statement to check for choice1:

var example= function(){
    else if (){

    }
    else if (){

    }
    else if (){

    }
}

inside each else if you make if/else to check for choic2:

var example= function(){
    else if (){
        if (){

        } else {

        }
    }
    else if (){
        if (){

        } else {

        }
    }
    else if (){
        if (){

        } else {

        }
    }
}

you need to follow this structure


#18

I made it thanks a lot !


#19

A post was split to a new topic: 7. what if choice1 is paper


#20

please help me find out the mistakes done below:
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";
}
/*var compare =function( userChoice , computerChoice)*/
console.log("User Choice: " + userChoice);

console.log("Computer: " + computerChoice);
/*var compare*/
var compare =function( userChoice , 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 === "rock"){
return "rock wins";
}
else{
return "scissors wins";
}
}

}

/*var compare =function( userChoice , computerChoice)

var userChoice = choice1;
var computerChoice = choice2;*/
console.log(userChoice, computerChoice);


#21

Please go go here, and make a new topic, this topic is for helping @objectplayer49046