-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
142 lines (96 loc) Ā· 3.81 KB
/
Copy pathmain.js
File metadata and controls
142 lines (96 loc) Ā· 3.81 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
// console.log(`Rock Paper Scissors Shoot!
// The game will go on for 5 rounds whoever have maximum points after 5 rounds wins.
// You get 1 point for winning round.
// If you lose computer get 1 point,
// For Tie you get 1 point each:
// Let's Play: `);
let humanScore = 0;
let computerScore = 0;
let humanChoice;
const result = document.querySelector(".result");
const buttons = document.querySelector(".buttons");
const rock = document.querySelector("#rock");
const paper = document.querySelector("#paper");
const scissors = document.querySelector("#scissors");
const human = document.querySelector(".human .who");
const bot = document.querySelector(".computer .who");
function getComputerChoice () {
let computerChoice = Math.random();
if (computerChoice < 0.34) {
return `scissors`;
}else if (computerChoice < 0.67) {
return `rock`;
}else {
return `paper`;
}
}
buttons.addEventListener("click", (event) => {
let target = event.target;
humanChoice = target.id;
botChoice = getComputerChoice();
playRound(humanChoice, botChoice);
if (humanChoice === "rock"){
human.textContent = `${humanChoice}`;
bot.textContent = botChoice;
}else if (humanChoice === 'scissors'){
human.textContent = humanChoice;
bot.textContent = botChoice;
}else {
human.textContent = humanChoice;
bot.textContent = botChoice;
}
});
function playRound (humanChoice, computerChoice){
if (humanChoice === computerChoice) {
humanScore++;
computerScore++;
result.textContent =`This round got Tied!
Score: Player: ${humanScore} --- Computer: ${computerScore}`;
}else if ((humanChoice === 'rock' && computerChoice === 'scissors')
|| (humanChoice === 'paper' && computerChoice === 'rock')
|| (humanChoice === 'scissors' && computerChoice === 'paper')){
humanScore++;
result.textContent = `You won this round!
Score: Player: ${humanScore} --- Computer: ${computerScore}`;
}else {
computerScore++;
result.textContent = `You lose this round!
Score: Player: ${humanScore} --- Computer: ${computerScore}`;
}
if (humanScore === 5) {
result.textContent = "";
result.style.color = "darkgreen";
result.textContent = `Hurrah!! You won this game!
FinalScore: You: ${humanScore} ---- Bot: ${computerScore}\n
Game Over!!\n
Reload to restart!`;
humanScore = 0;
computerScore = 0;
}else if(computerScore === 5) {
result.textContent = "";
result.style.color = "red";
result.textContent = `Sad! You lost this game!
Final Score: You: ${humanScore} ---- Bot: ${computerScore}\n
Game Oer!!!\n
Reload to restart!`;
}
}
// function playGame () {
// for (i = 1; i <= 5; i++){
// let humanSelection = getHumanChoice();
// let computerSelection = getComputerChoice();
// console.log(playRound(humanSelection, computerSelection));
// }
// if(humanScore > computerScore){
// console.log(`You won the game!Let's goo!šš
// Final Score: You: ${humanScore} --- Bot: ${computerScore}`);
// }else if(humanScore === computerScore) {
// console.log(`Game is Tied! Woahh soo close!
// Final Score: You: ${humanScore} --- Bot: ${computerScore}`);
// }else {
// console.log(`You lose! the game! Sad!!! šš
// Final Score: You: ${humanScore} --- Bot: ${computerScore}`);
// }
// console.log(`Thanks for playing!!`);
// }
// playGame();