-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
79 lines (73 loc) · 1.9 KB
/
javascript.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
var cscore = 0;
var pscore = 0;
function computerPlay(){
let num = Math.floor(Math.random() * 3);
if(num == 0){
return "rock";
}
else if(num == 1){
return "paper";
}
else{
return "scissors"
}
}
function playRound(playerSelection, computerSelection){
if(playerSelection == computerSelection){
return "It's a tie !";
}
else{
if(playerSelection == "paper"){
if(computerSelection == "rock"){
pscore++;
return "Player wins! Paper beats rock"
}
else{
cscore++;
return "Computer wins! Scissors beats paper"
}
}
else if(playerSelection == "scissors"){
if(computerSelection == "paper"){
pscore++;
return "Player wins! Scissors beats paper"
}
else{
cscore++;
return "Computer wins! Rock beats scissors"
}
}
else{
if(computerSelection == "scissors"){
pscore++;
return "Player wins! Rock beats scissors"
}
else{
cscore++;
return "Computer wins! Paper beats rock"
}
}
}
}
function buttonPlay(pstr){
let str = computerPlay();
console.log(playRound(pstr, str));
result.innerText = pscore + " - " + cscore;
if(pscore == 5){
pscore = 0;
cscore = 0;
result.innerText = "Player wins the match !";
}
else if(cscore == 5){
pscore = 0;
cscore = 0;
result.innerText = "Computer wins the match !";
}
}
const buttons = document.querySelectorAll('button');
const result = document.getElementById('result');
buttons.forEach((button) => {
button.addEventListener('click', () => {
buttonPlay(button.id);
});
});