Need help with Rock Paper Scissors Projct


#1

Hi guys, I have just finished my RPS project. Although the script kinda works, I would like to have some suggestions on my code. If you look at the code, you will notice that the function game() is repeated multiple times by calling it as many times. Is there a better way to repeat the function without literally typing it as many times?
Thanks in advance.

Here is my code:

Rock Paper Scissor
        let playerScore = 0
        let computerScore = 0
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        game()
        if (playerScore === 5) {
                alert ("You Win!")
            } else if (computerScore === 5) {
                alert ("You Lose!")
            }
    function game() {
            let playerSelection = prompt("Please enter 'Rock, Scissor, or paper.'")
            let output = ["rock", "paper", "scissor"]
            let computerSelection = output[Math.floor(Math.random() * output.length)]
            rps(playerSelection,computerSelection)
        function rps(playerSelection, computerSelection) {
            if (playerSelection === "rock" && computerSelection === "paper") {
                 alert("You Lose! " + computerSelection + " beats " + playerSelection + ".")
                 computerScore++
            } else if (playerSelection === "paper" && computerSelection === "scissor") {
                 alert("You Lose! " + computerSelection + " beats " + playerSelection + ".")
                 computerScore++
            } else if (playerSelection === "scissor" && computerSelection === "rock") {
                 alert("You Lose! " + computerSelection + " beats " + playerSelection + ".")
                 computerScore++
            } else if (playerSelection === "rock" && computerSelection === "scissor") {
                 alert("You Win! " + playerSelection + " beats " + computerSelection + ".")
                 playerScore++
            } else if (playerSelection === "paper" && computerSelection === "rock") {
                 alert("You Win! " + playerSelection + " beats " + computerSelection + ".")
                 playerScore++
            } else if (playerSelection === "scissor" && computerSelection === "paper") {
                 alert("You Win! " + playerSelection + " beats " + computerSelection + ".")
                 playerScore++
            } else {
                 alert("It's a tie. Please try again.")
            }
        }
    }
    </script>
</body>

#2

Hello @vinhduyhua

You can use the loop statements to execute the code repetitively.

Maybe you can try calling the “game” function within other function that calls it specified number of times.
For example like below:

function repeat_game(count)
{
    for (let i = 0; i < count; i++)
    {
        game();
    }
}

Henry


#3

Here’s some documentation for JavaScript loops.


#4

it works, thanks @henry