const choices = ["rock", "paper", "scissors"]; function getComputerChoice() { return choices[Math.floor(Math.random() * 3)]; } function playRound(playerChoice, computerChoice) { let playerChoiceCapital = playerChoice.slice(0,1).toUpperCase() + playerChoice.slice(1,playerChoice.length) let computerChoiceCapital = computerChoice.slice(0,1).toUpperCase() + computerChoice.slice(1,computerChoice.length) if (playerChoice == computerChoice) { return {string: `It's a tie! ${playerChoiceCapital} vs ${computerChoiceCapital}.`, points: {player: true, computer: true}}; } else if ((playerChoice == "rock" && computerChoice == "paper") || (playerChoice == "paper" && computerChoice == "scissors") || (playerChoice == "scissors" && computerChoice == "rock")) { return {string: `You lost! ${computerChoiceCapital} beats ${playerChoiceCapital}.`, points: {player: false, computer: true}}; } else if ((playerChoice == "rock" && computerChoice == "scissors") || (playerChoice == "paper" && computerChoice == "rock") || (playerChoice == "scissors" && computerChoice == "paper")) { return {string: `You won! ${playerChoiceCapital} beats ${computerChoiceCapital}.`, points: {player: true, computer: false}}; } } function game() { let points = { player: 0, computer: 0, } for (let i = 0; i < 5; i++) { let correctOption = false let playerChoice; while (correctOption == false) { playerChoice = prompt("What do you choose, 'rock', 'paper' or 'scissors'?").toLowerCase().trim(); choices.forEach(element => { if (playerChoice == element) { correctOption = true return; } }) } let round = playRound(playerChoice, getComputerChoice()); if (round.points.player) { points.player++; } if (round.points.computer) { points.computer++; } let pointsString = `Points: ${points.player}:${points.computer}\n`; alert(pointsString + round.string); } } game();