Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (120 loc) · 3.17 KB
/
Copy pathscript.js
File metadata and controls
149 lines (120 loc) · 3.17 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
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
//better woud have been ki choice class sare symbol ko dete aur indivisual ko alag id
//then use querySelector (".choices") on wwhich you can use forEach and add event Listener
let msgpara = document.querySelector(".msg");
let divsymbol=document.querySelector(".symbol");
let symbols=divsymbol.children; //to access inside div elements dont use childnodes
let score1=document.querySelector(".score1");
let score2=document.querySelector(".score2");
let score1No=score1.querySelector("#no");
let score2No=score2.querySelector("#no");
let btn=document.querySelector(".reset");
//so that initially they dont apply
msgpara.classList.remove("red");
msgpara.classList.remove("green");
function computerChoice()
{
let CompChoice=1+Math.floor(Math.random()*3); //generate number btw 1,2,3
console.log("Computer:",CompChoice);
return CompChoice;
}
function givemessage(winner,loser,signal)
{
if(signal==1)
{
msgpara.innerText=`You Won. ${winner} beats ${loser}`;
msgpara.classList.remove("red");
msgpara.classList.add("green");
console.log(msgpara.classList);
score1No.innerText++;
}
else
{
msgpara.innerText=`You Lost. ${winner} beats ${loser}`;
msgpara.classList.remove("green");
msgpara.classList.add("red");
console.log(msgpara.classList);
score2No.innerText++;
}
}
function game(choice,value)
{
let choiceComp=computerChoice();
console.log("my choice",choice," ",value);
if(choice==choiceComp)
{
msgpara.innerText="Game was drawn.Play Again!";
msgpara.classList.remove("red");
msgpara.classList.remove("green");
return;
}
if(choice==1)
{
if(choiceComp==2)
{
givemessage("paper",value,0);
}
if(choiceComp==3)
{
givemessage(value,"scissor",1);
}
return;
}
if(choice==2)
{
if(choiceComp==1)
{
givemessage(value,"rock",1);
}
if(choiceComp==3)
{
givemessage("scissor",value,0);
}
return;
}
if(choice==3)
{
if(choiceComp==1)
{
givemessage("rock",value,0);
}
if(choiceComp==2)
{
givemessage(value,"paper",1);
}
return;
}
}
for(let symbol of symbols)
{
console.log(symbol);
symbol.addEventListener("click",(e)=>
{
console.log(symbol.getAttribute("class"));
let classofsymbol=symbol.getAttribute("class");
if(classofsymbol=="rock")
{
console.log("rock");
game(1,classofsymbol);
}
else if(classofsymbol=="paper")
{
console.log("paper");
game(2,classofsymbol);
}
else
{
console.log("scissor");
game(3,classofsymbol);
}
})
}
console.log(btn.children[0]);
btn.onclick=(()=>
{
score1No.innerText=0;
score2No.innerText=0;
console.log("Hello");
msgpara.innerText="Pick Your Move";
msgpara.classList.remove("red");
msgpara.classList.remove("green");
})