Rock, paper or scissors - Part 2 Curly Bracket Understanding?


#1


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


Curious as to why the curly bracket functions in this manner. Don't quite understand.


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 <= 0.67) { // <---------THIS CURLY BRACKET THIS LOCATION MATCHES ALL ABOVE BUT GIVES ERROR "SyntaxError: Unexpected token {"
    computerChoice = "scissors";
}




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 <= 0.67) // <--- FUNCTIONS HERE, WHY BREAK PATTERN ABOVE?
    computerChoice = "scissors";
}


#2

Else statements don't take a condition. They just execute a code when previous if conditions aren't met. "Scissors" would be assigned to any computerChoice value over .67.


#3

Makes sense, thanks @biff75


#4

Just wondering, why did you initially assign the same condition to your else if and else code blocks?


#5

I was still troubleshooting the code and I had left the condition the same. I forgot about it when working on the bracket issue. It took it a correct but after the bracket was fixed. I went back and corrected to "=".


#6

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