Stuck on Rock Paper Scissors Project


#1

Hey guys. This is embarrassing, but I can’t figure out how to do the Rock Paper Scissors project.

I know that computerSelection and playerSelection are returning the values I want, but I cannot get my if else if statements to work properly.

function computerPlay(){
let computerHand = Math.random();
if (computerHand <= .30){
computerHand = “rock”;
} else if (computerHand <= .60) {
computerHand = “paper”;
} else {
computerHand = “scissors”;
}

return computerHand;
}

function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() === “rock” && computerSelection === “paper”){
return(“Computer wins!”);
} else if (playerSelection.toLowerCase() === “rock” && computerSelection === “scissors”) {
return(“Player wins!”);
} else if (playerSelection.toLowerCase() === “rock” && computerSelection === “paper”) {
return(“Computer wins!”);
} else if (playerSelection.toLowerCase() === “paper” && computerSelection === “rock”) {
return(“Player wins!”);
} else if (playerSelection.toLowerCase() === “paper” && computerSelection === “scissors”) {
return(“Computer wins!”);
} else if (playerSelection.toLowerCase() === “scissors” && computerSelection === “paper”) {
return(“Player wins!”);
} else if (playerSelection.toLowerCase() === “scissors” && computerSelection === “rock”) {
return(“Computer wins!”);
}else {
return(“IT’S A TIE!!!”);
}

}

const playerSelection = “PAPER”;
const computerSelection = computerPlay();
console.log(playerSelection.toLowerCase());
console.log(computerPlay());

console.log(playRound(playerSelection, computerSelection));


#2

Hello, I tested your code and your computerPlay() and playRound() functions are both working as intended. I think the reason you aren’t seeing the results properly is because of this line: console.log(computerPlay());
This is calculating a new hand. Replace it with console.log(computerSelection); and everything should be working fine.


#3

Oh my goodness I look like a fool… Thank you!
May I ask why console.log(computerPlay()); is returning the next hand and not the current one?


#4

it is because you are running the function twice.


#5

So the function in and of itself runs once, then calling the function name through console.log(); runs it for a second time? Am I understanding that correctly?


#6

From your code above;

const computerSelection = computerPlay(); ★run the function once
console.log(computerPlay()); ★ and here run the function second time


#7

Ohhhhhhhhhhhhhh.

Crystal clear.

Thank you both so much for taking the time to explain.


#8

:slight_smile: