Help! What if choice 1 is paper? please!


#1


What if choice1 is paper?


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


#2

What you are doing wrong is you are closing the else if statement before you put else, you need to close else if after you add else

Like so:

else if (condition) {
  if (condition) {
    // do something 
  } 
  else {
    // do something else
  }
}

#3

Isn't that what I was doing?
Sorry I really can't tell what I'm doing wrong.


#4

Yes you're right, but here's your mistake:

^ see here you're missing closing bracket for else if


#5

Don't I already have one?


#6

The last one closes else

else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
} //closes if
else {
return "paper wins";
} //closes else
//wheres closing bracket for else if statement?

#7

I have 3 curved brackets there though??


#8


#9

not talking about the last else if statement ..
the else if statement before that..


#10


#11

Don't paste in screen shots as I can't see full code..
this is what the syntax should look like:

else if (condition) {
  if (condition) {
    // do something 
  } /closes if
  else {
    // do something else
  } //closes else
} //closes else if

I added comments to make it easier to understand, DON'T rush take a few minutes read properly and apply.


This is the bit of code that needs adjusting:

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

#12

Oh thank you so much, because if I write heaps of code it gets tricky for me to see what's wrong with it, thanks again.


#13

No problem, you need to slow down and look carefully it can be overwhelming :slight_smile:


#14