Problem 3


#1

else if(choice1 === "rock") {

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

syntax error??


#2

Add one "}" before "else" and remove the last "}"

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

#3

could you please rewrite the code correctly to be understandable? Thanks and sorry


#4

again wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


#5

Then show the whole code.


#6

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


#7

Remove the first bracket here:

and here:

Add 2 more brackets at the end.


#8

You end your function too early so the else if part is not included in it.

@julietaatanasova

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

this way else would be the default for any input but it should only be the default for if(choice2 === "scissors") { @stelios200 initial approach on this was actually correct (if it would have been part of the function).


#9

i cant do it........


#10

Why? You were almost there. The general idea behind this is the following you have 3 possible options and rock,paper and scissors and the opponent has as well 3 possible choices leaving you with 9 possible results. To simplify this a bit we check for tie as one case instead of three and in the progress focus on player1 first:

var compare = function(choice1,choice2){
    if tie case
    else if choice1 is rock ... 
    else if choice1 is paper ... 
    else if choice1 is scissors ...
}

(Pseudo code not runnable)
Now what you do where i dotted out is you have a look at what player2 might choose. As tie is already out you're left with two options and as a possible example I quote you:


#11

I did it thanks.But i did not actually understand the meaning of brackets.I know the game and the optionw but its difficult codding with brackets {}


#12

The brackets indicate where your statements start and end. For example you have a whole bunch of statement and you want to make sure that they all belong to one function, now what you do is you wrap them in {}. If you format your code well it might actually make it easier to read your code.


#13

Thank you..did The second option start with "else if" or "if"


#14

does?
I am greek, so my english..


#15

Doesn't matter in this case. Normally using 4 ifs would mean that you have 4 conditions that can be picked independent of the each other, whereas else if chains them all together to one statement where only one option can be picked (the first condition that is true). But in this case every case has a return statements so that the function is exited at this point. Meaning you can, just like in an else if, only get to the next condition if the previous one was false so to quote the beginning here it doesn't matter.

But I'd suggest using else if here because this way it's more likely to get errors. Sounds strange doesn't it?. But if something is wrong, knowing that there is a bug in your code is better than having the feeling there might be something wrong with it but having no clue what it is.

Also I'm german so I won't jugde your english, I make enough mistakes myself but if the solution is good few people remind me of them :smile: