6/9 what's wrong? i checked many times but i dont know the error.please help


#1

/*here my code*/

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


#2

@sokoljosifi u should not be using elseif, instead use if, coz by using elseif u r not letting other condition work, as we want rock===scissors check to.


#3

it doesn't work even if i put 'if' instead of 'else if' ......:frowning:


#4

At first glance this seems to be correct. What is the problem with it? Do you get an error message, or does it behave unexpected? Have you tried refreshing the page? Often helps if no real error can be found as it resets the javaScript on the page (not your code).

@ankurlvv What should be the problem with the else if? From what I can see they should be ok.


#5

@haxor789 it means code also gonna work with, what @sokoljosifi is using. I thought it was incorrect. Thanks for pointing out.


#6

Yes that both works, in case there is still a confusion think about it this way:

var test = 3

if(test >1){console.log("A");}
else if(test >2){console.log("B");}

if(test >1){console.log("A");}
if(test >2){console.log("B");}

in the first case only A is printed because you pick the first matching case. Whereas the second one would print both "A" and "B" because you have two conditions that both can be right independent from each other. The reason the second option works here as well is that you are inside of a function and use return. What happens is that after getting to a return statement you immediately leave the function and ignore the rest so the only way to reach "B" is if "A" was false so in the end this does the same thing that an else if would. But as said this is just because of the circumstances that you are inside of a function and use return.


#7

@haxor789 thanks buddy