|
| 1 | +// randomly returns rock, paper, or scissors for computer choice |
| 2 | + |
| 3 | +let humanScore = 0; |
| 4 | +let computerScore = 0; |
| 5 | +let result = ''; |
| 6 | +let round = 5; |
| 7 | + |
| 8 | + |
| 9 | +function capitalizeFirstLetter(string) { |
| 10 | + |
| 11 | + return string.charAt(0).toUpperCase()+string.slice(1).toLowerCase(); |
| 12 | + |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +function getComputerChoice() { |
| 17 | + |
| 18 | + const randomNum = Math.random(); |
| 19 | + |
| 20 | + if (randomNum <= 1/3 ) { |
| 21 | + return 'Rock'; |
| 22 | + } |
| 23 | + else if ( 1/3 < randomNum && randomNum <= 2/3){ |
| 24 | + return 'Paper'; |
| 25 | + } |
| 26 | + else if (2/3 < randomNum && randomNum <= 1) { |
| 27 | + return 'Scissors' |
| 28 | + } |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +function getHumanChoice() { |
| 34 | + |
| 35 | + let userChoice = capitalizeFirstLetter(prompt("Please enter your choice (Rock, Paper, Scissors)")); |
| 36 | + |
| 37 | + while (!(userChoice === 'Rock' || userChoice === 'Paper' || userChoice === 'Scissors')) { |
| 38 | + |
| 39 | + userChoice = capitalizeFirstLetter(prompt("Please enter your choice (Rock, Paper, Scissors)")); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + return userChoice; |
| 44 | +} |
| 45 | + |
| 46 | +function playRound (humanChoice, computerChoice) { |
| 47 | + |
| 48 | + |
| 49 | + if(humanChoice === 'Rock' && computerChoice === 'Scissors') { |
| 50 | + result = 'You Win!'; |
| 51 | + } |
| 52 | + else if(humanChoice === 'Rock' && computerChoice === 'Paper') { |
| 53 | + result = 'You Lose!'; |
| 54 | + } |
| 55 | + else if(humanChoice === 'Paper' && computerChoice === 'Rock') { |
| 56 | + result = 'You Win!'; |
| 57 | + } |
| 58 | + else if(humanChoice === 'Paper' && computerChoice === 'Scissors') { |
| 59 | + result = 'You Lose!'; |
| 60 | + } |
| 61 | + else if(humanChoice === 'Scissors' && computerChoice === 'Paper') { |
| 62 | + result = 'You Win!'; |
| 63 | + } |
| 64 | + else if(humanChoice === 'Scissors' && computerChoice === 'Rock') { |
| 65 | + result = 'You Lose!'; |
| 66 | + } |
| 67 | + else if(humanChoice === computerChoice){ |
| 68 | + result = 'tie'; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + if(result === 'You Win!') { |
| 73 | + humanScore++; |
| 74 | + } |
| 75 | + else if(result === 'You Lose!') { |
| 76 | + computerScore++; |
| 77 | + } |
| 78 | + |
| 79 | + console.log(`Your choice ${humanChoice}, Computer Choice ${computerChoice}, ${result}`); |
| 80 | + console.log(`Your Score ${humanScore}, Computer Score ${computerScore}`) |
| 81 | +} |
| 82 | + |
| 83 | +function playGame(round) { |
| 84 | + |
| 85 | + for(let i = 1; i <= round; i++) { |
| 86 | + playRound(getHumanChoice(),getComputerChoice()); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +playGame(round) |
0 commit comments