-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (57 loc) · 1.87 KB
/
Copy pathscript.js
File metadata and controls
68 lines (57 loc) · 1.87 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
function getComputerChoice () {
let computerChoice = ['Rock', 'Paper', 'Scissors'];
// return computerChoice[Math.floor(Math.random() * 3)].toLowerCase();
play = computerChoice[Math.floor(Math.random() * 3)].toLowerCase();
console.log('computer plays ' + play);
return play;
}
function getPlayerChoice () {
let playerChoice = prompt('Rock, paper, scissors, shoot!');
return playerChoice.toLowerCase();
}
function playRound (playerSelection, computerSelection) {
playerSelection = getPlayerChoice();
computerSelection = getComputerChoice();
if (playerSelection == computerSelection) {
return 0;
} else if (playerSelection == 'rock' && computerSelection == 'scissors') {
return 1;
} else if (playerSelection == 'scissors' && computerSelection == 'paper') {
return 1;
} else if (playerSelection == 'paper' && computerSelection == 'rock') {
return 1;
} else if (computerSelection == 'rock' && computerSelection == 'scissors') {
return 2;
} else if (computerSelection == 'scissors' && playerSelection == 'paper') {
return 2;
} else if (computerSelection == 'paper' && playerSelection == 'rock') {
return 2;
} else {
return 3;
}
}
function playGame () {
let playerScore = 0;
let computerScore = 0;
let i = 1;
while (i <= 5) {
let result = playRound();
if (result == 1) {
playerScore++
} else if (result == 2) {
computerScore++
} else if (result == 0) {
i--
}
console.log('Round:' + i + ' Human:' + playerScore + ' Computer:' + computerScore);
if (playerScore == 3) {
console.log ('You win!');
return
} else if (computerScore == 3) {
console.log ( 'You lose.' );
return
}
i++
}
}
playGame();