8. What if choice1 is scissors?


#1

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

unexpected token else

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

compare(userChoice, computerChoice)


#2

That error message means you should go through your code involving else and else if statements carefully. Make sure the syntax is correct.


#3

Is there anything in particular I should look for?


#4

Look at your last else if code block. How does it differ from the 2 before it?


#5

The only difference I saw was it was missing/the others had extra curly brackets. I fixed that and it still didn't work


#6

As you make adjustments to code, other errors may come up. Post what you have so far.


#7

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

}
}
compare(userChoice, computerChoice)


#8

Also, how do you close a function?


#9

Disposition of Curly Brackets.
See below syntax for your reference:

On your first if/else/else if under the declaration of your variable var computerChoice = Math.random();

if (condition here) {
   //block of code here
   }
else if(condition here) {
   //block of code here
  }
else {
   //block of code here
  }

Furthermore, you need to close your function with a closing curly bracket, see below syntax..

var compare = function() {
        if () {
            return "";
        }
        else if () {
            if (){
                return "";
                }
            else { 
                return "";
                }
        }
        else if (){
            if () { 
                return ""; 
                }
             else { 
                return ""; 
                } 
        }
    };

Hope this helps.


#10

Thanks but I still get the same error


#11

Post your updated code and kindly use preformatted text, click this tag </> in posting your code.


#12

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

}
}
};
compare(userChoice, computerChoice)


#13

Have you examined the syntax i gave? again, i suggest, have another look...

if (condition here) { //Opening curly bracket of IF
   //block of code here
   } // Closing curly bracket of IF
else if(condition here) { //Opening curly bracket of ELSE IF
   //block of code here
  } //Closing curly bracket of ELSE IF
else { //Opening curly bracket of ELSE
   //block of code here
  } //Closing curly bracket of ELSE

and for your function:

var compare = function() { //Opening curly bracket of your FUNCTION
        if () { //Opening curly bracket of IF
            return "";
        } //Closing curly bracket of 1st IF
        else if () { //Opening curly bracket of 1st ELSE IF
            if (){ //Opening curly bracket of IF inside youre 1st ELSE IF
                return "";
                } //Closing curly bracket of IF inside youre 1st ELSE IF
            else { //Opening curly bracket of ELSE inside youre 1st ELSE IF
                return "";
                } //Closing curly bracket of ELSE inside youre 1st ELSE IF
        } //Closing curly bracket of 1st ELSE IF
        else if () { //Opening curly bracket of 2nd ELSE IF
            if (){ //Opening curly bracket of IF inside youre 2nd ELSE IF
                return "";
                } //Closing curly bracket of IF inside youre 2nd ELSE IF
            else { //Opening curly bracket of ELSE inside youre 2nd ELSE IF
                return "";
                } //Closing curly bracket of ELSE inside youre 2nd  ELSE IF
        } //Closing curly bracket of 2nd ELSE IF
    }; //Closing curly bracket of your FUNCTION

#14

same error

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

}
}
};
compare(userChoice, computerChoice)


#16

You wrote: if(choice1 === choice2) {
return "The result is a tie";

--Where is your "}" to close the "if" statement?

you wrote: else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}

--where is your "}" to close your "else if" statement?


#17

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