-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
80 lines (75 loc) · 2.94 KB
/
main.js
File metadata and controls
80 lines (75 loc) · 2.94 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
77
78
79
80
var rock = document.getElementById('rock');
var paper = document.getElementById('paper');
var scissors = document.getElementById('scissors');
var roundResult = document.getElementById('roundResult');
var playerPoints = 0;
var computerPoints = 0;
var finalResult = document.getElementById('finalResult');
var computer = document.getElementById('computer');
var player = document.getElementById('player');
var divPlays = document.getElementById('plays');
// Play selection event listener
rock.addEventListener('click', playRound);
paper.addEventListener('click', playRound);
scissors.addEventListener('click', playRound);
// Function playRound
function playRound(e) {
var playerSelect = e.target.id;
var computerSelect = computerPlay();
if (playerSelect === computerSelect) {
return roundResult.textContent = 'Tied round';
} else if (playerSelect === 'rock') {
if (computerSelect === 'paper') {
computerPoints++;
computer.textContent = computerPoints;
updateFinalResult();
return roundResult.textContent = 'You lose this round! Paper beats Rock';
} else {
playerPoints++;
player.textContent = playerPoints;
updateFinalResult();
return roundResult.textContent = 'You win this round! Rock beats Scissors';
}
} else if (playerSelect === 'paper') {
if (computerSelect === 'rock') {
playerPoints++;
player.textContent = playerPoints;
updateFinalResult();
return roundResult.textContent = 'You win this round! Paper beats Rock';
} else {
computerPoints++;
computer.textContent = computerPoints;
updateFinalResult();
return roundResult.textContent = 'You lose this round! Scissors beat Paper';
}
} else {
if (computerSelect === 'rock') {
computerPoints++;
computer.textContent = computerPoints;
updateFinalResult();
return roundResult.textContent = 'You lose this round! Rock beats Scissors';
} else {
playerPoints++;
player.textContent = playerPoints;
updateFinalResult();
return roundResult.textContent = 'You win this round! Scissors beat Paper';
}
}
}
// Function computerPlay
function computerPlay() {
var availablePlays = ['rock', 'paper', 'scissors'];
return availablePlays[Math.floor(Math.random() * availablePlays.length)];
}
// Update game result
computer.textContent = computerPoints;
player.textContent = playerPoints;
function updateFinalResult() {
if (playerPoints === 5) {
finalResult.textContent = 'You win the game. Congratulations!';
plays.style.display = 'none';
} else if (computerPoints === 5) {
finalResult.textContent = 'You lose the game. Next time!';
plays.style.display = 'none';
}
}