Game works sometimes - but not if I choose paper


#1


Hi everyone,

I'm having trouble figuring out why my game doesn't always work. Sometimes it will just show the console log of Computer's choice, but not a result of the game. I don't have any error messages. I'm guessing it may have something to do with how I am using } and ;. I am not sure how to use these properly, so help with that as well as any other mistakes you see would be great!

Thank you-


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 (userChoice,computerChoice) 
    {
        if (userChoice===computerChoice) 
        {return "The result is a tie!";
            }
        else if (userChoice==="rock")
                {if (computerChoice==="scissors")
                return "rock wins";}
        else if (userChoice==="paper")
            {
                if (computerChoice==="rock")
             return "paper wins";
        else if (userChoice==="scissors")
            {
                if (computerChoice==="rock")
              return "rock wins"}
        else
            return "scissors wins";
            }
}
compare(userChoice,computerChoice);


#2

It would seem that your code has just gotten a little sloppy in formatting. I have compared your code to mine and it looks fine I think it just needs to be cleaned up. You have all the pieces of the right code but your set up in the if else if and else statements is a little odd so that might be your problem like you had suggested. I would suggest comparing your set up in organization to my code:

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

Normally, I never give out full code. However, it is going to be hard in this case to help you reorganize it like I think that might help you. Since you have all the pieces I believe this is ok. If you still have trouble after this we can work through this in more detail to see what the issue is.


#3

Thank you so much @bibleman13. I look forward to troubleshooting this further when I get home from work!


#4

Glad to hear it! I am glad I was able to help and I hope it goes smoothly at home! Happy coding :smiley:


#5


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 (userChoice,computerChoice)
{
if (userChoice===computerChoice) {
return "The result is a tie!";
}
else if (userChoice==="rock") {
if (computerChoice==="scissors")
return "rock wins";
}
else if (userChoice==="paper") {
if (computerChoice==="rock")
return "paper wins";
}
else if (userChoice==="scissors") {
if (computerChoice==="rock")
return "rock wins";
}
else if (userChoice==="paper") {
if (computerChoice="scissors")
return "scissors wins";
}
else if (userChoice="rock") {
if (computerChoice="paper")
return "paper wins";
}
else
return "scissors wins";
}
compare(userChoice,computerChoice);

Thanks again for your reply the other day! I started to clean up my code and format it in a more standard way with the curly brackets being on the same line as the else if statement.

I found another mistake that I can't quite figure out:
If computerChoice= paper, the only thing that happens is the console logs the Computer's choice. I thought maybe this was because I didn't have any statements that explained what to do if computer choice was paper, so I added in the last set of statements before the else statement. This didn't do anything to my kind of surprise.

Any suggestions?

Many thanks!


#6

I ended up solving this some how, but I don't know why it works! Beginner's luck perhaps :slight_smile:


#7

Yeah I investigated your code and sure enough I had the same problem I was not able to find anything that cleared it so I am glad you were able to! :smiley: May I ask what you changed? I am curious now lol


#8

Here's the final code that I used - I have to admit I am not even fully sure what I changed, so if you could help me to see that would be amazing!

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("Your choice: " + userChoice)
console.log("Computer choice: " + computerChoice);
var compare = function (userChoice,computerChoice) 
    {
        if (userChoice===computerChoice) {
            return "The result is a tie!";
        }
        else if (userChoice==="rock") {
            if (computerChoice==="scissors")
            return "rock wins";
            else {
                return "paper wins"
            }
        }
        else if (userChoice==="paper") {
            if (computerChoice==="scissors")
            return "scissors wins";
            else {
                return "paper wins"
            }
        }
        else if (userChoice==="scissors") {
            if (computerChoice==="paper")
            return "scissors wins";
        else {
            return "rock wins"
        }
        }
    }
compare(userChoice,computerChoice);

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.