What if choice 1 = paper?


#1


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


"unexpected token else" These curly braces are killing me!! Ugh.. I don't think I understand how many are required, and where exactly they go/don't go....


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


#2

I got it. Still not exactly sure what I changed to make it correct, but I'm gonna look it over and see exactly where the difference lies. (EDIT: I realize I did not close out the paper wins; with a { after it. And previously had 4 curly braces at the end, where I only needed three. I'm glad I actually figured it out rather than just tweaking and moving curly braces around randomly. I hope this helps someone else who gets stuck on this exercise.

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


#3

var compare = function(choice1, choice2) { // curly 1a
  if (choice1 === choice2) { // curly 2a
    return "The result is a tie!";
  } // curly 2b
  else if(choice1 === "rock") { // curly 3a
    if (choice2 === "scissors") { // curly 4a
      return "rock wins";
    } // curly 4b
    else { // curly 5a
      return "paper wins";
    } // curly 5b
  else if(choice1 === "paper") { // curly 6a
    if(choice2 === "rock") { // curly 7a
      return "paper wins";
    else { // curly 8a
      return "scissors wins";
    } // curly 8b
  } // curly 6b
} // curly ?
} // curly ?


// counting our curly brackets
/*
1a
2a2b
3a
4a4b
5a5b
6a6b
7a
8a8b
*/

This is my conclusion assigning each of your curly's id's in order of occurence and matching that occurence with a matching closing pair. You can take it from here.


Build Rock, Paper, Scissors... 7. What if Choice 1 = paper
#4

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