I Need Assistance With Chapter 8 "What if choice1 is Scissors"


#1



"There was a problem with your Syntax"
"Syntax Error: Unexpected Token {"


It seems that the problem with my code is that I am missing a curly bracket somewhere. But I can't seem to find the proper location of where I messed up. I might have even messed up more areas so if someone can point out what I did right and wrong I'd appreciate it.


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

compare(computerChoice + " " + userChoice)

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


#2

Hi there. Your code is mostly fine, one thing you don't need (and the primary reason why this current code won't run) is the condition in the final else statement.

The point of the else statement is to just capture all of the undefined behaviour (or in this example - other answers) you might come across.

You can fix this in two ways. One would be to remove the (choice1 === "scissors") part after the else. The other way would be to change that final else to else if` so that you always make the check for "scissors"

Another thing - I'd advise you to always be consistent in formatting your code. E.g. if you put a closing bracket behind else on the same row, do that for every single else in your code. Also, be generous with spaces in javascript - put a space after every return and before every opening bracket. Readability is key to finding (and ultimately fixing) those little pesky "bugs".