forked from atishay95/Rock-Paper-Scissors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (135 loc) · 3.85 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var rock = document.querySelector("#rock");
var paper = document.querySelector("#paper");
var scissors = document.querySelector("#scissors");
var lizard = document.querySelector("#lizard");
var spock = document.querySelector("#spock");
var count1 = document.querySelector('#count1');
var count2 = document.querySelector('#count2');
var userChoice;
var computerChoice;
function compChoice()
{
console.log("inside the computer choice fn");
computerChoice = Math.random();
if (computerChoice < 0.20)
{
alert("computer chose rock");
return "rock";
}
else if(computerChoice <= 0.40)
{
alert("computer chose paper");
return "paper";
}
else if(computerChoice <= 0.60)
{
alert("computer chose scissors");
return "scissors";
}
else if(computerChoice <= 0.80)
{
alert("computer chose lizard");
return "lizard";
}
else
{
alert("computer chose spock");
return "spock";
}
}
function usChoice()
{
rock.addEventListener("click", () => {
alert("you chose rock");
alert(compare("rock", compChoice()));
});
paper.addEventListener("click", () => {
alert("you chose paper");
alert(compare("paper", compChoice()));
});
scissors.addEventListener("click", () => {
alert("you chose scissors");
alert(compare("scissors", compChoice()));
});
lizard.addEventListener("click", () => {
alert("you chose lizard");
alert(compare("lizard", compChoice()));
});
spock.addEventListener("click", () => {
alert("you chose spock");
alert(compare("spock", compChoice()));
});
}
function compare(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
if(choice1 === "rock")
{
if(choice2 === "scissors" || choice2 === "lizard")
{
count1.innerHTML=++count1.innerHTML;
return "you win";
}
else
{
count2.innerHTML=++count2.innerHTML;
return "computer wins";
}
}
if(choice1 === "paper" )
{
if(choice2 === "rock" || choice2 === "spock")
{
count1.innerHTML=++count1.innerHTML;
return "you win";
}
else
{
count2.innerHTML=++count2.innerHTML;
return "computer wins";
}
}
if(choice1 === "scissors")
{
if(choice2 === "paper" || choice2 === "lizard")
{
count1.innerHTML=++count1.innerHTML;
return "you win";
}
else
{
count2.innerHTML=++count2.innerHTML;
return "computer wins";
}
}
if(choice1 === "lizard")
{
if(choice2 === "paper" || choice2 === "spock")
{
count1.innerHTML=++count1.innerHTML;
return "you win";
}
else
{
count2.innerHTML=++count2.innerHTML;
return "computer wins";
}
}
if(choice1 === "spock")
{
if(choice2 === "scissors" || choice2 === "rock")
{
count1.innerHTML=++count1.innerHTML;
return "you win";
}
else
{
count2.innerHTML=++count2.innerHTML;
return "computer wins";
}
}
};
usChoice();