-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (74 loc) · 3.01 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
const selectionBtn = document.querySelectorAll('.selectionBtn');
const result = document.querySelector('.result');
const array = ['Rock', 'Paper', 'Scissors'];
function getComputerChoice() {
const randomIndex = Math.floor(Math.random()*3)
return array[randomIndex];
}
function getUserChoice(event) {
const userInput = event.target.value;
return userInput;
}
let humanScore = 0;
let computerScore = 0;
function playRound(e) {
const humanChoice = getUserChoice(e);
const computerChoice = getComputerChoice();
result.textContent = '';
if(humanChoice === computerChoice) {
const par1 = document.createElement('p');
par1.textContent = `It's a tie! Let's go again!`;
const par2 = document.createElement('p');
par2.textContent = `Your Score: ${humanScore}`;
const par3 = document.createElement('p');
par3.textContent = `Computer Score: ${computerScore}`;
result.append(par1, par2, par3);
} else if((humanChoice === 'Rock' && computerChoice=== 'Scissors') || (humanChoice=== 'Paper' && computerChoice=== 'Rock') || (humanChoice=== 'Scissors' && computerChoice=== 'Paper')) {
const par1 = document.createElement('p');
par1.textContent = `You won! ${humanChoice} beats ${computerChoice}!`;
const par2 = document.createElement('p');
humanScore++;
par2.textContent = `Your Score: ${humanScore}`;
const par3 = document.createElement('p');
par3.textContent = `Computer Score: ${computerScore}`;
result.append(par1, par2, par3);
} else if((humanChoice=== 'Rock' && computerChoice=== 'Paper') || (humanChoice=== 'Paper' && computerChoice=== 'Scissors') || (humanChoice=== 'Scissors' && computerChoice=== 'Rock')) {
const par1 = document.createElement('p');
par1.textContent = `You lost! ${computerChoice} beats ${humanChoice}!`;
const par2 = document.createElement('p');
computerScore++;
par2.textContent = `Your Score: ${humanScore}`;
const par3 = document.createElement('p');
par3.textContent = `Computer Score: ${computerScore}`;
result.append(par1, par2, par3);
}
}
function play(e) {
if(humanScore === 5 || computerScore === 5) {
return;
}
playRound(e);
if(humanScore === 5 || computerScore === 5) {
const finalResult = document.createElement('div');
if(humanScore === 5) {
finalResult.textContent = `You won the Game!`;
} else if (computerScore === 5) {
finalResult.textContent = `You lost the Game!`;
}
result.appendChild(finalResult);
const restartBtn = document.createElement('button');
restartBtn.textContent = 'Restart';
restartBtn.addEventListener('click', resetGame);
result.appendChild(restartBtn);
}
}
function resetGame() {
humanScore = 0;
computerScore = 0;
result.textContent = '';
}
selectionBtn.forEach((btn) => {
btn.addEventListener('click', (event) => {
play(event);
});
})