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