-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
112 lines (100 loc) · 3.78 KB
/
index.html
File metadata and controls
112 lines (100 loc) · 3.78 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
<!DOCTYPE html>
<body>
<button type="button" class="choices" id="Rock">Rock</button>
<button type="button" class="choices" id="Paper">Paper</button>
<button type="button" class="choices" id="Scissors">Scissors</button>
<div id="results">
</div>
<div id="score">
</div>
<script>
function computerPlay(){
//test
let number = Math.floor(Math.random()*3);
if (number === 0){
return 'Rock';
}
if (number === 1){
return 'Paper';
}
if (number === 2){
return 'Scissors'
}
}
function playerSelection(){
//No default text could result in issues***
let choice = window.prompt("Choose 'Rock', 'Paper', or 'Scissors'").toLowerCase();
return choice.charAt(0).toUpperCase() + choice.slice(1);
}
function playRound(e){
playerSelection = e.srcElement.id;
computerSelection = computerPlay();
console.log(playerSelection)
console.log(computerSelection);
if (playerSelection === computerSelection){
updateScore("Tie. " + playerSelection + " ties " + computerSelection);
}
if((playerSelection === 'Rock') && (computerSelection === "Scissors")){
playerScore++
updateScore("You Win! Rock beats Scissors");
}
if((playerSelection === 'Rock') && (computerSelection === "Paper")){
computerScore++
updateScore("You Lose! Paper beats Rock");
}
if((playerSelection === 'Paper') && (computerSelection === "Rock")){
playerScore++
updateScore("You Win! Paper beats Rock");
}
if((playerSelection === 'Paper') && (computerSelection === "Scissors")){
computerScore++
updateScore("You Lose! Scissors beats Paper");
}
if((playerSelection === 'Scissors') && (computerSelection === "Paper")){
playerScore++
updateScore("You Win! Scissors beats Paper");
}
if((playerSelection === 'Scissors') && (computerSelection === "Rock")){
computerScore++
updateScore("You Lose! Rock beats Scissors");
}
}
function updateScore(outcome){
document.getElementById("results").innerHTML = outcome;
document.getElementById("score").innerHTML = `Player: ${playerScore} || Computer: ${computerScore}`;
checkWin();
}
function checkWin(){
if(computerScore == 5){
alert("You Lose!");
computerScore = 0;
playerScore = 0;
}
if(playerScore == 5){
alert("You Win!");
computerScore = 0;
playerScore = 0;
}
}
function game(){
//console.log(playRound(playerSelection(), computerPlay()));
if(playerScore < computerScore){
console.log("You lose :(");
}
else if(playerScore == computerScore){
console.log("You tied :/");
}
else{
console.log("You win!!!");
}
}
let playerScore = 0;
let computerScore = 0;
let buttons = document.querySelectorAll('.choices');
buttons.forEach((btn) => {
btn.addEventListener("click", playRound);
});
game();
</script>
</body>
</html>