-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.html
81 lines (75 loc) · 2.66 KB
/
rps.html
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
<!doctype html>
<html>
<body style="text-align: center">
<div id="buttons" style="height: 50px; margin-top: 30px;margin-bottom: 10px;">
<button style="position: absolute; width: 100px; margin-left: -50px; left: 50%;">Rock</button>
<button style="position: absolute; width: 100px; margin-left: -50px; left: 40%;">Paper</button>
<button style="position: absolute; width: 100px; margin-left: -50px; left: 60%;">Scissors</button>
</div>
<div id="result">
<h1 style="text-align:center"> Results</h1>
<p style="text-align: center "> </p>
</div>
<script>
let computerscore = 0;
let playerscore = 0;
function computerPlay() {
let num = Math.floor(Math.random() * 3);
if (num == 0) {
return "Rock";
}
else if (num == 1) {
return "Paper"
}
else if (num == 2) {
return "Scissors"
}
}
function playRound(player, cpu) {
let mine = player.toUpperCase();
let computer = cpu.toUpperCase();
let div = document.querySelector("#buttons");
let div1 = document.querySelector("#result")
let body = document.querySelector("body");
let paragraph = document.querySelector("p");
let end = document.createElement("h1");
if ((computer == "ROCK" && mine == "SCISSORS")
|| (computer == "PAPER" && mine == "ROCK")
|| (computer == "SCISSORS" && mine == "PAPER")) {
paragraph.textContent = "You chose " + mine + ". Computer chose " + computer + ". You lose!";
computerscore += 1;
}
else if ((computer == "PAPER" && mine == "SCISSORS")
|| (computer == "SCISSORS" && mine == "ROCK")
|| (computer == "ROCK" && mine == "PAPER")) {
paragraph.textContent = "You chose " + mine + ". Computer chose " + computer + ". You win!";
playerscore += 1;
}
else {
paragraph.textContent = paragraph.textContent = "You chose " + mine + ". Computer chose " + computer + ". You draw!";
}
if (computerscore == 5) {
body.removeChild(div);
body.removeChild(div1);
end.textContent = "You Lose!";
body.appendChild(end);
}
else if (playerscore == 5) {
body.removeChild(div);
body.removeChild(div1);
end.textContent = "You Win!";
body.appendChild(end);
}
else {
paragraph.textContent = paragraph.textContent + " Score: " +
playerscore + "- " + computerscore
}
}
const buttons1 = document.querySelectorAll("button");
buttons1.forEach((button) => {
button.addEventListener("click", (e) =>
playRound(button.textContent, computerPlay()));
});
</script>
</body>
</html>