7. Whats if choice1 is paper?


#1

It keeps saying unexpected token, and I've looked on other forms, but I can't figure out my error.
Here's my code

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

(its super sloppy I know)


#2
try this one its works for me

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

#3

Wow the sloopyness :laughing:! You can beautify your code here

Now the reason you didnt get the problem correct is because you had your brackets misplaced. And you didnt find it because you didnt organize your code. After i organize the code here is the solution!:

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(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    if (choice1 === "rock") {
        if ((choice2 === "scissors")) {

            return ("rock wins");
        } else {
            return ("paper wins");
        }
    } else

    {
        if (choice1 === "paper") {

            if (choice2 === "scissors") {

                return ("scissors wins");
            } else {
                return ("paper wins");
            }
        }



    }
}

#4

Try opening up your code to full nesting. The problem that you ran into is that you closed your function in the middle of the code.

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";}}} ///too many }'s here/// else if
(choice1 === "paper") {if (choice2 === "rock") {return "paper wins";}
else {return "scissors win";};}

try laying it out like this:

var compare = function(choice1, choice2) { ///opens function
if (choice1 === choice2) { ///opens if
    return "The result is a tie!";
} ///closes the if
else if (choice1 === "rock") { ///opens else if
    if (choice2 === "scissors") ///opens if
        return "rock wins";
    } ///closes if
    else { ///opens else
        return "paper wins";
    } ///closes else
} ///closes else if
else if (choice1 === "paper") { ///opens else if
    if (choice2 === "rock") { ///opens if
        return "paper wins";
    } ///closes if
    else { ///opens else
        return "rock wins";
    } ///closes else
} ///closes else if
}; ///closes functio

hope this helps


#5

How are yo color coding you color coding your code?


#6

before copying or typing the code put 3 ` marks 1 line above and one line below the code. Those tick marks are the ones located right above the Tab button on your keyboard


#7

`
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";}}} ///too many }'s here/// else if
(choice1 === "paper") {if (choice2 === "rock") {return "paper wins";}
else {return "scissors win";};}

`

Dosent work..


#8

you need 3 of those ticks on one line above and three on one line below


#9

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";}}} ///too many }'s here/// else if
(choice1 === "paper") {if (choice2 === "rock") {return "paper wins";}
else {return "scissors win";};}

Got it :smile:


#10

So many mistakes in this code looking at it again :sweat: Thanks again!


#11

Hey no problem! Here is a place where you can practice even more javascript!!!! And sharpen your skills


#12

Hi, could you check this please?
It keeps saying "SyntaxError: Unexpected keyword 'else'"
Thanks!

/*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 (choise2 === "rock") {
        return "paper wins";
    }}
    else {
        return "scissors wins";
    }
    }

            return "scissors wins";
        }
}

#13

2 problems that I see. you have: if (choise2 === "rock) { instead of: if (choice2 === "rock") {
and you have two "}" after the "return "paper wins"; instead of 1. Hope this helped


#14

I need looooot of practice. Thanks!