-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
95 lines (86 loc) · 2.65 KB
/
script.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
function getComputerChoice()
{
let computerChoice;
const choiceNum = Math.floor(Math.random() * 3);
if (choiceNum===0)
{
computerChoice="rock";
}
else if(choiceNum===1)
{
computerChoice="paper";
}
else
{
computerChoice="scissors";
}
return computerChoice;
}
function playRound(playerSelection, computerSelection)
{
if(playerSelection === computerSelection)
{
draw(playerSelection);
}
else if ( //win round
(playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'scissors' && computerSelection === 'paper') ||
(playerSelection === 'paper' && computerSelection === 'rock')
) {
win(playerSelection,computerSelection);
if(pScore===5)
{
final.textContent = "You Win!";
scoreBoard.appendChild(final);
return;
}
}
else if ( //lose round
(computerSelection === 'rock' && playerSelection === 'scissors') ||
(computerSelection === 'scissors' && playerSelection === 'paper') ||
(computerSelection === 'paper' && playerSelection === 'rock')
) {
lose(playerSelection,computerSelection);
if(compScore===5)
{
final.textContent = "You Lose.\n Refresh to Try again";
scoreBoard.appendChild(final);
return;
}
}
}
function win(pChoice,compChoice)
{
pScore++;
const player = document.querySelector("p.player");
player.textContent = `Player :${pScore}`;
result.textContent = `You Win! ${pChoice.charAt(0).toUpperCase() + pChoice.slice(1)} beats
${compChoice.charAt(0).toUpperCase() + compChoice.slice(1)}`;
return;
}
function lose(pChoice,compChoice)
{
compScore++;
const comp = document.querySelector("p.computer");
comp.textContent = `Computer :${compScore}`;
result.textContent = `You Lose! ${compChoice.charAt(0).toUpperCase() + compChoice.slice(1)} beats
${pChoice.charAt(0).toUpperCase() + pChoice.slice(1)}`;
return;
}
function draw(choice)
{
result.textContent = `You Draw! ${choice.charAt(0).toUpperCase() + choice.slice(1)} draws
${choice.charAt(0).toUpperCase() + choice.slice(1)}`;
return;
}
let pScore=0;
let compScore=0;
const scoreBoard = document.querySelector('div.scoreBoard');
const result = document.querySelector("h3"); //result of each round
const final = document.createElement("h2"); //final result
const buttons = document.querySelectorAll("button");
buttons.forEach((button) => button.addEventListener("click", () =>
{
playRound(button.id,getComputerChoice());
}
));