-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (87 loc) · 3.15 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let playerWins = 0;
let computerWins = 0;
let ties = 0;
function computerPlay(){
let moveRandom = Math.floor(Math.random() * 3);
let computerMove;
if (moveRandom === 0) {
computerMove = 'rock'
} else if (moveRandom === 1) {
computerMove = 'paper'
} else {
computerMove = 'scissors'
}
return computerMove
}
function playRound(playerSelection){
let outcome;
let computerSelection = computerPlay()
if (playerSelection === computerSelection) {
outcome = 'Its a tie!';
ties++;
} else if (playerSelection === 'rock' && computerSelection === 'scissors'){
outcome = 'You win! Rock beats scissors!';
playerWins++;
} else if (playerSelection === 'rock' && computerSelection === 'paper'){
outcome = 'You lose! Paper beats rock!';
computerWins++;
} else if (playerSelection === 'paper' && computerSelection === 'rock'){
outcome = 'You win! Paper beats rock!';
playerWins++;
} else if (playerSelection === 'paper' && computerSelection === 'scissors'){
outcome = 'You lose! Scissors beats paper!';
computerWins++;
} else if (playerSelection === 'scissors' && computerSelection === 'paper'){
outcome = 'You win! Scissors beats paper!';
playerWins++;
} else if (playerSelection === 'scissors' && computerSelection === 'rock'){
outcome = 'You lose! Rock beats scissors!';
computerWins++;
} else {
outcome = 'Please input rock, paper or scissors...'
}
console.log(outcome);
roundCounter();
}
function roundCounter() {
let rounds = playerWins + computerWins + ties;
if (rounds === 5){
if (playerWins > computerWins) {
console.log("You win the game! Resetting!");
resetGame();
} else {
console.log("You lost the game! Better luck next time...resetting...");
resetGame();
}
}
}
function resetGame() {
playerWins = 0;
computerWins = 0;
ties = 0;
}
const container = document.querySelector('#container');
let playerScore = document.createElement('div');
playerScore.classList.add('playerScore');
container.appendChild(playerScore)
let computerScore = document.createElement('div');
computerScore.classList.add('computerScore');
container.appendChild(computerScore)
const rockButton = document.getElementById('rock');
rockButton.addEventListener('click', function () {
playRound('rock');
playerScore.textContent = `Your score: ${playerWins}`;
computerScore.textContent = `Computer score: ${computerWins}`;
});
const paperButton = document.getElementById('paper');
paperButton.addEventListener('click', function () {
playRound('paper');
playerScore.textContent = `Your score: ${playerWins}`;
computerScore.textContent = `Computer score: ${computerWins}`;
});
const scissorsButton = document.getElementById('scissors');
scissorsButton.addEventListener('click', function () {
playRound('scissors');
playerScore.textContent = `Your score: ${playerWins}`;
computerScore.textContent = `Computer score: ${computerWins}`;
});