-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
96 lines (82 loc) · 2.6 KB
/
javascript.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
function getComputerChoice() {
let choice = Math.random();
let s = '';
if (choice > 0.34) {
s = 'rock';
}
else if (choice > 0.67) {
s = 'paper';
}
else {
s = 'scissors';
}
return s;
}
function playRound(playerSelection, computerSelection) {
let answer;
playerSelection = playerSelection.toLowerCase();
if (playerSelection !== 'scissor' || playerSelection !== 'paper' || playerSelection !== 'rock')
answer = 'You Lose, Invalid Input!'
if (playerSelection == 'scissors' && computerSelection == 'paper')
answer = 'You Win! Scissor beats paper';
if (playerSelection == computerSelection)
answer = 'Its a Draw!';
if (playerSelection == 'scissors' && computerSelection == 'rock')
answer = 'You Lose! Rock beats Scissors';
if (playerSelection == 'paper' && computerSelection == 'scissors')
answer = 'You Lose! Scissors beats Paper';
if (playerSelection == 'paper' && computerSelection == 'rock')
answer = 'You Win! Paper beats Rock';
if (playerSelection == 'rock' && computerSelection == 'scissors')
answer = 'You Win! Rock beats Scissors';
return answer;
}
function game(playSel) {
let compSel = getComputerChoice();
let round = playRound(playSel, compSel);
return round;
}
let gameStarted = false;
let gameScreen = document.getElementById('active');
let startScreen = document.getElementById('start');
let startText = startScreen.querySelector('.start-text')
startText.addEventListener('click', () => {
gameStarted = true;
checkState();
});
let message;
function checkState() {
if (gameStarted) {
document.querySelector('.score').innerHTML = '';
message = '';
gameScreen.style.display = 'flex';
startScreen.style.display = 'none';
rock = document.getElementById('rock');
rock.addEventListener('click', () => {
message = game('rock');
document.querySelector('.score').innerHTML = '';
document.querySelector('.score').append(message)
gameStarted = false;
checkState();
})
paper = document.getElementById('paper');
paper.addEventListener('click', () => {
message = game('paper');
document.querySelector('.score').innerHTML = '';
document.querySelector('.score').append(message)
gameStarted = false;
checkState();
})
scissors = document.getElementById('scissors');
scissors.addEventListener('click', () => {
message = game('scissors');
document.querySelector('.score').innerHTML = '';
document.querySelector('.score').append(message)
gameStarted = false;
checkState();
})
} else {
gameScreen.style.display = 'none';
startScreen.style.display = 'flex';
}}
checkState();