-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
122 lines (107 loc) · 3.79 KB
/
javascript.js
File metadata and controls
122 lines (107 loc) · 3.79 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
function getComputerChoice() {
let compChoice;
let num = Math.floor(Math.random() * 3);
num == 0
? (compChoice = "Rock")
: num == 1
? (compChoice = "Paper")
: (compChoice = "Scissors");
return compChoice;
}
let humanScore = 0;
let computerScore = 0;
const rock = document.querySelector(".rock");
const paper = document.querySelector(".paper");
const scissors = document.querySelector(".scissors");
const choice = document.createElement("li");
const text = document.createElement("li");
const Score = document.createElement("li");
const list = document.querySelector("ul");
rock.addEventListener("click", () => {
if (humanScore == 5 || computerScore == 5) {
if (humanScore > computerScore) {
alert("YOU WON!!");
humanScore = 0;
computerScore = 0;
} else {
alert("COMPUTER WON!!");
humanScore = 0;
computerScore = 0;
}
}
const ROCK = "ROCK";
playRound(ROCK, getComputerChoice);
});
paper.addEventListener("click", () => {
if (humanScore == 5 || computerScore == 5) {
if (humanScore > computerScore) {
alert("YOU WON!!");
humanScore = 0;
computerScore = 0;
} else {
alert("COMPUTER WON!!");
humanScore = 0;
computerScore = 0;
}
}
const PAPER = "PAPER";
playRound(PAPER, getComputerChoice);
});
scissors.addEventListener("click", () => {
if (humanScore == 5 || computerScore == 5) {
if (humanScore > computerScore) {
alert("YOU WON!!");
humanScore = 0;
computerScore = 0;
} else {
alert("COMPUTER WON!!");
humanScore = 0;
computerScore = 0;
}
}
const SCISSORS = "SCISSORS";
playRound(SCISSORS, getComputerChoice);
});
function playRound(humChoice, computerChoice) {
compChoice = computerChoice();
humChoice = humChoice.toUpperCase();
compChoice = compChoice.toUpperCase();
list.appendChild(choice);
list.appendChild(text);
list.appendChild(Score);
if (compChoice === humChoice) {
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "its a tie both chose " + humChoice;
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "ROCK" && humChoice === "PAPER") {
humanScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You WON! Paper beats Rock !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "ROCK" && humChoice === "SCISSORS") {
computerScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You LOST! Rock beats Scissors !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "PAPER" && humChoice === "ROCK") {
computerScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You LOST! Paper beats Rock !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "PAPER" && humChoice === "SCISSORS") {
humanScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You WON! Scissors beats Paper !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "SCISSORS" && humChoice === "PAPER") {
computerScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You LOST! Scissors beats Paper !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
} else if (compChoice === "SCISSORS" && humChoice === "ROCK") {
humanScore++;
choice.textContent = `Computer Chose : ${compChoice}`;
text.textContent = "You WON! Rock beats Scissors !!";
Score.textContent = `You - ${humanScore} computer - ${computerScore} `;
}
}