What if choice1 is scissors? - Build "Rock, Paper, Scissors"


#1

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"
}
}
if (choice2 === "rock") {
return "paper wins"
} else {
return "scissors wins"
}
};

compare(userChoice, computerChoice);

This is my code, and whenever type in something as user, forexample "rock" in the prompt it returns = Computer: paper
"paper wins"

But now the wierd thing it says this, Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and rock

I thought my code were right?


#2

Hi you what if choice one is scissors should look like that

else if (choice1 === "scissors"){
    if statement  {
    
    }
    else statement  {
   
    }
}

#3

Where should i put it? Can you try edit my code so it works so i can see an example for it? :smiley: Thanks!


#4

In wich exercise are you?


#5

This part...

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

Should be like that

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

#6

ok i am having the same issue that projectalioskar is having, I also went through this thread and read what everyone said and (attempted) to correct all the issues that were mentioned here yet I am having the same error message,

"But now the wierd thing it says this, Oops, try again. Your code returned 'paper wins' instead of 'undefined' when the inputs are scissors and paper"

Here is the complete code that I currently have:
`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!";}
}
else if(choice1==="paper"){
if(choice2==="rock"){
return "paper wins";}
else{
return "scissors wins";}
}
else if(choice1 === "scissors"){
if(choice2==="rock"){
return "rock wins";}
else{
return "paper wins";}
}
}
compare(userChoice,computerChoice);`


#7

Hi you should close all your else if statement }


#8

Are they not all closed? I just went through and checked, both by making sure every open bracket has a close bracket and also by trying to add 1 more close bracket at the end. both of which came up with the conclusion that i have the right number of open/close brackets.


#10

wouldnt the } after "return "paper wins";}" be the one that closes the else statement? so the first bracket on the line below it closes the else if statement then the one on the second line closes the function?


#11

also since adding the brackets my program went from "working" with an error message to syntax error.


#12

Yes I didn't see it and yes it like you say


#13

`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";
}
}
else if(choice1==="paper")
{
if(choice2==="rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
else if(choice1 === "scissors")
{
if(choice2==="rock")
{
return "rock wins";
}
else
{
return "paper wins";
}
}
}
compare(userChoice,computerChoice);`

here is another post of my code that has all my brackets on their own line to help people see where they open/close


#14

On you originale code here

else{
return "paper wins!";}
}

Just remove the !


#15

Here

else{
return "paper wins!";}
}

Just remove the ! inside the string


#16

alright I did that, and updated the code above. Still have the same error message


#17

Try to change your last else statement by

return "scissors wins"

#18

The error cannot come from display messages. OMG.
Who have thought this system if and else, very sick person, there is no way to follow the syntax, and the choices are not logically grouped, on the basis of which you take the potential situations as is recomended

we should have 3 CLEAR groups: (the 3 POSIBILITIE/ALTERNATIVES)
rock scissors
rock paper
paper - scissors
and therefore to take into consideration that each can be either choice1 either choice2
so using math we have combinations of 3 elements grouped by any two of them


#19

This actually fixed my issue.

Thank you very much :slightly_smiling:


#20

Your welcome I'm glad that I could help you.


#21

This is the fair code:
krazychemist you are referring in two situations of to the same combination of choices (1st and 3rd check choice 1 and two)

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";}
}
else if(choice1==="paper"){
if(choice2==="rock"){
return "paper wins";}
else{
return "scissors wins";}
}
else if(choice1 === "scissors"){
if(choice2==="paper"){
return "scissors wins";}
else{
return "rock wins";}
}
}
compare(userChoice,computerChoice)