8. What if choice 1 is scissors? So many curly brackets


#1


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


I think i've missplaced a curly bracket somewhere but my eyes hurt trying to find it.
Error message is "SyntaxError: expected expression, got keyword '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(userChoice,computerChoice) {

if(choice1 === choice2) {
      return "The result is a tie!";
}

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


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

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

};
compare (userChoice, computerChoice);


#2

The two arguments for your main function should be (choice1, choice2)


#3

Thanks - changed that but still get the same message.


#4

This is your code without the errors.

var compare = function(choice1, choice2) {

if(choice1 === choice2) {
      return "The result is a tie!";
}

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

    else                            {
        return "paper wins";
                                    }
}


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

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

};
compare(userChoice, computerChoice);

Your code had a lot of errors one being excessive curly brackets

, semi colons after declaring else if statements

, and defining your function incorrectly.


#5

thanks - I think i just got so frustrated i kept adding stuff - what i should probably have done was start from scratch


#6

Yes! that would have been the best idea. :sweat_smile:


#7

same... you're not alone (@ the kept adding stuff out of frustration comment =])


#9

I was in the same boat - confusing as ■■■■. But then...I decided to Reset the Code, Select, Copy. Open Dreamweaver, Paste. Rework. The UI was kinda throwing me off. This made it a bit easier, with room for the code to breathe and eased my mind into seeing it properly.


#10

Should´nt it be "userChoice" and "computerChoice" instead of "choice1" and "choice2" ?


#11

Not in the function, but when calling the function those are the two parameters you would pass.


#12

yes in the function. He has to give it a name. It doenst matter if that name is choice1 or userChoice. But if he made a decision about the name, he has to keep that name in all calls.


#13

choice1 and choice2 are the parameters he is required to use so he can pass the SCT.


#14

He declared that variables.

There he used new variables wich were never declared. This cant work.


#17

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