i’m trying to make a rock-paper.. game but it seems that the code works only when i have a tie. I’ve probably messed it up on the way down.Also i want to ask if the number on the prompt window that you fill in is a string or a number ?
Any bit of help appreciated.Thanks !
// rock beats scissors (1 beats 3)
// paper beats rock (2 beats 1)
// scissors beat paper (3 beat 2)
var player1= prompt("Player ONE, choose (1) for rock, (2) for paper, (3) for scissors");
var player2 = prompt("Player TWO, choose (1) for rock, (2) for paper, (3) for scissors");
function game (player1,player2)
{
if (player1===player2){
alert("its a tie");
}
else
{
if (player1+player2==="4")
{
if(player1==="1"){
alert("Rock beats Scissors, Player one wins");
}else {
alert("Rock beats Scissors, Player Two wins");
}
}
if (player1+player2==="3")
{
if (player1==="1"){
alert("paper beats rock, player One wins");
}else {
alert ("paper beats rock, player Two wins");
}
}
if (player1+player2==="5")
{
if (player1==="3"){
alert("scissors beats paper, Player One wins");
}else{
alert("scissors beats papaer, player Two wins");
}
}
}
};
game(player1,player2);
You’re concatenating strings, not adding numbers, so your
player1+player2==="3"would actually produce12or21. You want to convert the string to a number first.Place this code at the top of the else block of your tie check.
As an extension, you will want to sanitise the player input to make sure that it only contains digits, as this method will fail if anything other than a numeric string is passed.