-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (96 loc) · 3.66 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
let player = 0;
let computer = 0;
// computerPlay function will randomly returns rock, paper, or scissors
function computerPlay() {
let getRandom = Math.floor(Math.random() * 10);
if (getRandom <= 3) {
return "Rock";
} else if (getRandom <= 6) {
return "Paper";
} else {
return "Scissors";
}
}
let text = document.querySelector("#text");
text.textContent = "";
function playRound(playerSelection, computerSelection) {
//('running here', playerSelection, computerSelection)
playerSelection = playerSelection.toString();
let getPlayerInsensitive = playerSelection.toLowerCase();
let getResult = document.getElementById("result");
if (getPlayerInsensitive === "rock" && computerSelection === "Rock") {
player+=0;
text = `Player: ${player} Computer: ${computer}
It's a tie, both are Rock`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "rock" && computerSelection === "Paper") {
computer+=1;
text = `Player: ${player} Computer: ${computer}
You Lose! Paper beats Rock`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "rock" && computerSelection === "Scissors") {
player+=1;
text = `Player: ${player} Computer: ${computer}
You win! Rock beats Scissors`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "paper" && computerSelection === "Rock") {
player+= 1;
text = `Player: ${player} Computer: ${computer}
You win! Paper beats Rock`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "paper" && computerSelection === "Paper") {
player+=0;
text = `Player: ${player} Computer: ${computer}
It's a tie! Both are Paper`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "paper" && computerSelection === "Scissors") {
computer+=1;
text = `Player: ${player} Computer: ${computer}
You lose! Scissors beats Paper`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Rock") {
computer+=1;
text = `Player: ${player} Computer: ${computer}
You lose! Rock beats Scissors`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Scissors") {
player+=0;
text = `Player: ${player} Computer: ${computer}
It's a tie! Both are Scissors`;
getResult.innerHTML = text;
} else if (getPlayerInsensitive === "scissors" && computerSelection === "Paper") {
player+=1;
text = `Player: ${player} Computer: ${computer}
You win! Scissors beat Paper`;
getResult.innerHTML = text;
} else {
return "Check your spelling!";
}
if(player === 5){
document.querySelectorAll('button.select').forEach(elem => {
elem.disabled = true;
});
text = `Player: ${player} Computer: ${computer}
You've won ${player} games and your
opponent only won ${computer} games.
YOU WIN!`;
getResult.innerHTML = text;
} else if(computer === 5){
document.querySelectorAll('button.select').forEach(elem => {
elem.disabled = true;
});
text = `Player: ${player} Computer: ${computer}
You've only won ${player} games and
your opponent won ${computer} games.
YOU LOST!`;
getResult.innerHTML = text;
}
}
//event listeners
const result = document.querySelector("#result");
const rockBtn = document.querySelector("#rock");
const paperBtn = document.querySelector("#paper");
const scissorsBtn = document.querySelector("#scissors");
let rockClick = rockBtn.addEventListener('click', () => playRound("rock", computerPlay()));
let paperClick = paperBtn.addEventListener('click', () => playRound("paper", computerPlay()));
let scissorsClick = scissorsBtn.addEventListener('click', () => playRound("scissors", computerPlay()));