## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

# There is an undefined error in my Rock Paper Scissors game. Trying to figure out why.

I've been working on my own game after completing JavaScript Basics by Dave McFarland.

I'm pretty much finished, and it works about 75% of the time. Except there is something making the code execute the else statement about 25% of the time.

```    function compHand() {

var randomHand = Math.floor(Math.random() *3) + 1;

var compRock = "ROCK";
var compPaper = "PAPER";
var compScissors = "SCISSORS";

if (randomHand === 1){
return compRock;
}

if (randomHand === 2){
return compPaper;
}

if (randomHand === 3){
return compScissors;
}

}

function playerHand() {

var playerWin = "You Win";
var compWin = "You Lose";
var playerTie = "You Tie";

if (playerChoice.toUpperCase() === "ROCK" && compHand() == "SCISSORS" ) {
playerScore += 1
return playerWin

}

else if (playerChoice.toUpperCase() === "PAPER" && compHand() == "ROCK" ){
playerScore += 1
return playerWin

}

else if (playerChoice.toUpperCase() === "SCISSORS" && compHand() == "PAPER" ){
playerScore += 1
return playerWin

}

else if (playerChoice.toUpperCase() === "ROCK" && compHand() == "PAPER" ){
compScore += 1
return compWin

}

else if (playerChoice.toUpperCase() === "PAPER" && compHand() === "SCISSORS" ) {
compScore += 1
return compWin

}

else if (playerChoice.toUpperCase() === "SCISSORS" && compHand() === "ROCK" ){
compScore += 1
return compWin

}

else if (playerChoice.toUpperCase() === "SCISSORS" && compHand() == "SCISSORS" ){
return playerTie

}

else if (playerChoice.toUpperCase() === "ROCK" && compHand() == "ROCK" ){
return playerTie
}

else if (playerChoice.toUpperCase() === "PAPER" && compHand() == "PAPER" ){
return playerTie

}

else {
return("Something isn't quite working");
}

}

var playerScore = 0;
var compScore = 0;

var playerChoice = prompt("Please Choose a hand: Rock, Paper, or Scissors");

console.log(playerHand());
console.log(playerHand());
console.log(playerHand());
console.log(playerHand());
console.log(playerHand());
console.log(playerHand());

console.log("Computer Score: " + compScore);
console.log("Player Score: " + playerScore);
```

*I tried adding comments. It became a little buggy. let me know if there is a trick to it. Thank you.*

You have a logic problem. In playerHand you are checking the player hand against the computer value, but every time the compHand() function runs it returns a value that is potentially different from the check before:

player = rock

if(player == rock && compHand() == rock) -> compHand() can return paper here

else if(player == rock && compHand() == paper) -> and might return rock here

else if()

So it is very probable that the test will change from the first if statement to the last.

What I suggest is in playerHand() set a variable that is the value of compHand() and do the if check based on that:

```function playerHand() {
var playerWin = "You Win";
var compWin = "You Lose";
var playerTie = "You Tie";

var comp = compHand();

// WIN
if (playerChoice.toUpperCase() === "ROCK" && comp == "SCISSORS" ) {
playerScore += 1
return playerWin

}

else if (playerChoice.toUpperCase() === "PAPER" && comp == "ROCK" ){
playerScore += 1
return playerWin

}

else if (playerChoice.toUpperCase() === "SCISSORS" && comp == "PAPER" ){
playerScore += 1
return playerWin

}
// LOSE
else if (playerChoice.toUpperCase() === "ROCK" && comp == "PAPER" ){
compScore += 1
return compWin

}

else if (playerChoice.toUpperCase() === "PAPER" && comp === "SCISSORS" ) {
compScore += 1
return compWin

}

else if (playerChoice.toUpperCase() === "SCISSORS" && comp === "ROCK" ){
compScore += 1
return compWin

}
// TIES
else if (playerChoice.toUpperCase() === "SCISSORS" && comp == "SCISSORS" ){
return playerTie

}

else if (playerChoice.toUpperCase() === "ROCK" && comp == "ROCK" ){
return playerTie
}

else if (playerChoice.toUpperCase() === "PAPER" && comp == "PAPER" ){
return playerTie

}

else {
return("Something isn't quite working");
}
}
```

You then will have a situation where the compHand function only gets called once when the playerHand function gets called instead of 9 times.

Hey Joseph, really appreciate you helping me out so fast! It works great now.

I completely forgot about storing a function into a variable! You are the man!