Skip to content

Commit 69b8c9f

Browse files
committed
Implement Rock Paper Scissors console game logic
- Create Functions for computer and human selection. - Add input validation to ensure user types correct options. - Create 5-round game loop. - Fix casing add spelling issues for 'Scissors' logic.
1 parent 27bc721 commit 69b8c9f

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

Scripts/script.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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)

index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Rock Paper Scissors</title>
7+
</head>
8+
<body>
9+
10+
<script src="./Scripts/script.js">
11+
12+
</script>
13+
</body>
14+
</html>

script.js

Whitespace-only changes.

0 commit comments

Comments
 (0)