|
| 1 | +// console.log("hello"); |
| 2 | + |
| 3 | +let humanScore = 0; |
| 4 | +let computerScore = 0; |
| 5 | + |
| 6 | +function getComputerChoice() { |
| 7 | + let randomNumber = Math.floor(Math.random() * 3); //randomly generates a number between 0, 1, 2 |
| 8 | + |
| 9 | + if (randomNumber === 0) { |
| 10 | + return "Rock"; |
| 11 | + } else if (randomNumber === 1) { |
| 12 | + return "Paper"; |
| 13 | + } else { |
| 14 | + return "Scissors"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function getHumanChoice() { |
| 19 | + let choice = prompt("Choose between rock/paper/scissors."); |
| 20 | + if (choice === "rock") { |
| 21 | + return "Rock"; |
| 22 | + } else if (choice === "paper") { |
| 23 | + return "Paper"; |
| 24 | + } else if (choice === "scissors") { |
| 25 | + return "Scissors"; |
| 26 | + } else { |
| 27 | + return "Invalid option, choose between rock/paper/scissors."; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function playRound(humanChoice, computerChoice) { |
| 32 | + console.log(humanChoice, computerChoice); |
| 33 | + if (humanChoice === "Rock" && computerChoice === "Scissors") { |
| 34 | + humanScore++; |
| 35 | + console.log(`Final Score - Human: ${humanScore}, Computer: ${computerScore}`); |
| 36 | + return "You win! Rock beats Scissors."; |
| 37 | + } else if (humanChoice === "Paper" && computerChoice === "Rock") { |
| 38 | + humanScore++; |
| 39 | + console.log(`Final Score - Human: ${humanScore}, Computer: ${computerScore}`); |
| 40 | + return "You win! Paper beats Rock."; |
| 41 | + } else if (humanChoice === "Scissors" && computerChoice === "Paper") { |
| 42 | + humanScore++; |
| 43 | + return "You Win! Scissors beats Paper."; |
| 44 | + } else if (humanChoice === computerChoice) { |
| 45 | + console.log(`Final Score - Human: ${humanScore}, Computer: ${computerScore}`); |
| 46 | + return "It's a tie."; |
| 47 | + } else { |
| 48 | + computerScore++; |
| 49 | + console.log(`Final Score - Human: ${humanScore}, Computer: ${computerScore}`); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// const humanSelection = getHumanChoice(); |
| 54 | +// const computerSelection = getComputerChoice(); |
| 55 | + |
| 56 | +// console.log(playRound(humanSelection, computerSelection)); |
| 57 | + |
| 58 | +function playGame() { |
| 59 | + let rounds = 0; |
| 60 | + |
| 61 | + while (rounds < 5) { |
| 62 | + let newHumanChoice = getHumanChoice(); |
| 63 | + let newComputerChoice = getComputerChoice(); |
| 64 | + playRound(newHumanChoice, newComputerChoice); |
| 65 | + rounds++; |
| 66 | + } |
| 67 | + if (humanScore > computerScore) { |
| 68 | + return "You Win!"; |
| 69 | + } else if (humanScore < computerScore) { |
| 70 | + return "You Lose!"; |
| 71 | + } else { |
| 72 | + return "It's a draw."; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +console.log(playGame()); |
| 77 | + |
0 commit comments