-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (76 loc) · 3.38 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<title> Rock Paper Scissors </title>
</head>
<body>
<div class = 'btns'>
<button value = 'rock'> Rock </button>
<button value = 'paper'> Paper </button>
<button value = 'scissors'> Scissors </button>
</div>
<script>
function computerPlay() {
switch (Math.floor(Math.random()*3)){
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break
}
}
function playRound(playerSelection, computerSelection){
if (playerSelection === computerSelection){
return 'The game is a draw!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
return 'You lose! Paper beats rock!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
return 'You win! Paper beats rock!';
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
return 'You lose! Rock beats scissors!';
computerScore= computerScore+1;
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
return 'You win! Rock beats scissors!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
return 'You lose! Scissors beats paper!';
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
return 'You win! Scissors beats paper!';
}
}
const body = document.querySelector('body');
const score = document.createElement('div');
score.classList.add('score');
body.appendChild(score);
let myScore=0;
let computerScore=0;
const btns = document.querySelector('.btns');
btns.addEventListener('click', event => {
const playerSelection = event.target.value;
const computerSelection = computerPlay();
console.log('you threw ' + playerSelection);
console.log('computer threw ' + computerSelection);
console.log(playRound(playerSelection, computerSelection));
let result = playRound(playerSelection, computerSelection);
if (result === 'You win! Scissors beats paper!' || result=== 'You win! Rock beats scissors!' || result=== 'You win! Paper beats rock!'){
myScore = ++myScore;
console.log('Your score: ' + myScore + ' Computer score: ' + computerScore); ;
} else if (result === 'You lose! Scissors beats paper!' || result === 'You lose! Rock beats scissors!' || result === 'You lose! Paper beats rock!'){
computerScore = ++computerScore;
console.log('Your score: ' + myScore + ' Computer score: ' + computerScore);
} else if (result === 'The game is a draw!'){
console.log('Your score: ' + myScore + ' Computer score: ' + computerScore);
}
score.textContent = 'Your score: ' + myScore + ' Computer score: ' + computerScore;
if (myScore >= 5){
alert('You win');
} else if (computerScore >= 5) {
alert('You lost');
}
});
</script>
</body>
</html>