Rock, paper, scissors game!


#1

{if (choice1===choice2){return"The result is a tie!"};}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
}
else {
return "paper wins";
}
if (choice1==="paper"){
if(choice2==="scissors"){
return "scissors wins";
}
else{
return"paper wins";
};
}
if(choice1==="scissors"){
if(choice2==="rock"){
return"rock wins";
}
else{
return"scissors";
}

Why is my code not running? Please Help!


#2

@bytepro63044 you don't need the { } around your if statement at the beginning, but you also might want to count the number of opening and closing curly braces you have... it looks like they don't pair up.


#3

if (choice1===choice2){return"The result is a tie!"};}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
return "rock wins";
};
}
else {
return "paper wins";
}
if (choice1==="paper"){
if(choice2==="scissors"){
return "scissors wins";
}
else{
return"paper wins";
};
}
if(choice1==="scissors"){
if(choice2==="rock"){
return"rock wins";
}
else{
return"scissors";
};
}

Updated code. Is my spacing not correct?


#4

You made many mistakes. Instead of using many times "if", use "else if". There are some other small mistakes, mostly extra '{' and '}'. I corrected your code into the following:

var function_name = 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 === "scissors") {
    return "scissors wins";
    } else {
    return"paper wins";
    };
  } else {
    if(choice2==="rock"){
      return"rock wins";
    } else {
      return"scissors";
    };
  }
};
var x = prompt("What's your choice?");
var y = function(z) {
  if (z < 0.33) {
   return "rock"; 
  } else if (z > 0.33) {
   return "paper";
  } else {
    return "scissors";
  }
}
y(Math.random());
function_name(x, y);

This should work. It's autonomous, and it's against the computer. I don't know how you wanted to run the rock, paper, scissors game without calling a function or introducing data. Hope it helps :smile:


#5

I think in your first line...er
like this :smile:
return "The result is a tie!";