Rock, Paper, Scissors Function Issue

Ok, so I have completed the rock, paper, scissors project. It runs (yay), but it’s not looping correctly. It only takes one computer selection and one player selection and then console.logs the same choices five times. I’m sure it’s something simple I just can’t figure it out…Help! Here is the code I’m working with:

let playerScore = 0;
let computerScore = 0;

//>Get the computers play
function computerPlay() {
          let compChoice = Math.random();
          if (0 < compChoice < 0.33) {
            return "rock";
          } else if (0.34 < compChoice < 0.66) {
            return "paper";
          } else {
            return "scissors"; }
}
let computerSelection = computerPlay();

//>Get players choice and correct to compare
function userChoice() {
        let userChoice = prompt("Do you chose rock, paper, or scissors?");
        userChoice = userChoice.toLowerCase();
        if (userChoice == "rock" || userChoice == "paper" || userChoice == "scissors") {
            return userChoice
        } else {
            alert("Not a valid choice"); }
}

let playerSelection = userChoice(); 

//>get the winner of a round
 function playRound(playerSelection, computerSelection) {
          if (playerSelection === computerSelection) {
              return "Tie! Next Round!";
        } else if (playerSelection === "rock" && computerSelection === "paper") {
          computerScore++;
          return "You lose.";
        } else if (playerSelection === "paper" && computerSelection === "scissors") {
          computerScore++;
          return "You lose.";
        } else if (playerSelection === "scissors" && computerSelection === "rock") {
          computerScore++;
          return "You lose.";
        } else if (playerSelection === "rock" && computerSelection === "scissors") {
          playerScore++;
          return "You win!";
        } else if (playerSelection === "paper" && computerSelection === "rock") {
          playerScore++;
          return "You win!";
        } else if (playerSelection === "scissors" && computerSelection === "paper") {
          playerScore++;
          return "You win!"; }
}

//>create function to play the game
function playGame() {
    for (let roundsPlayed = 0; roundsPlayed < 5; roundsPlayed++) {
            playRound();        
            console.log(playerSelection);
            console.log(computerSelection);
            let result = playRound(playerSelection, computerSelection);
            console.log(result);
            const score = "Player Score: " + playerScore + " CPU Score: " + computerScore;
            console.log(score); }
}

//>Play the game by calling the function
playGame();

getting the computer and player selection happens once, if you wanted to generate new selections every round, the selection should happen within the loop

2 Likes

I knew it was something simple! Thanks for the help!