Rock paper scissors (WHAT IS WRONG IN THIS CODE ?)


#1

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

this code:

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

should be:

else if(choice1 ==="rock") {
  /* here */
}

the way you currently wrote your code, if choice2 is scicoors, it will be true, so rock always wins.


#3

@channuu,

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )
    //==========================================//

#4

Your title contains a lot of capital letters, which usually implies shouting. Could you please edit your title to lowercase?

Thanks,
JibblyJ.