8. What if choise1 is scissors?


#1

No idea what I'm doing wrong, this is what I have:

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

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

Seems like your missing a } that closes your function.


#3

Me, too. I'm not sure what I'm doing wrong either.

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

}
}
};

#4

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

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

your missing the closing } for the else if here. Probably that is why you went on with if instead of else if, doesn't you :wink:?
And maybe have a look at the hint of 6/9 for how such a case is structured as the second try is not as good as the first one.


#5

Clicking next without adding the required code seems to progress the lesson but of course that won't teach me anything.

@-@ I don't know what the problem is. First it says there's an extra 'else' then it says there's an extra '}' ;~; It's so confusing.


#6

There is a } missing before your second else if and after your second else if you have 2 additional }. Maybe indenting anything inside functions as well makes it clearer.


#7

I tried adding curly brackets and removing them. Still gives me an unexpected token for either 'else' or the '}'


#8

You're actually comparing "rock" as choice2, twice .

The final else if statement:

should be checking "scissors" vs. "paper".

EDIT: Not sure why my quote formatting is all messed up.


#9

The brackets at the end were the problem. Going through these exercises, I now have mad respect for developers. @-@