-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
139 lines (116 loc) · 4.55 KB
/
function.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
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
function getComputerChoice() {
const list = ['paper', 'scissor', 'rock'];
const randomIndex = Math.floor(Math.random() * list.length);
const randomString = list[randomIndex];
return randomString;
};
function playRound(playerSelection, computerSelection){
if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissor') {
return "You Lose! scissor beats paper";
} else if (playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') {
return "You Win! paper beats rock";
}
if (playerSelection.toLowerCase() === 'scissor' && computerSelection === 'paper') {
return "You Win! scissor beats paper";
} else if (playerSelection.toLowerCase() === 'scissor' && computerSelection === 'rock') {
return "You Lose! rock beats scissor";
};
if (playerSelection.toLowerCase() === 'rock' && computerSelection=== 'paper') {
return "You Lose! paper beats rock";
} else if (playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissor') {
return "You Win! rock beats scissor";
};
if (playerSelection.toLowerCase() === computerSelection) {
return "Draw! Same choice";
};
};
const outcome = {
"You Lose! scissor beats paper": 0,
"You Win! paper beats rock": 1,
"You Win! scissor beats paper": 1,
"You Lose! rock beats scissor": 0,
"You Lose! paper beats rock": 0,
"You Win! rock beats scissor": 1,
"Draw! Same choice": 2
};
let playerScore = 0;
let computerScore = 0;
let drawScore = 0;
// // function keeps score from playRound game
// function game(playerChoice){
// const computerSelection = getComputerChoice();
// let winnerLoser = playRound(playerChoice, computerSelection);
// console.log(winnerLoser);
// if (outcome[winnerLoser] === 1){
// playerScore += 1;
// } if (outcome[winnerLoser] === 0){
// computerScore += 1;
// }
// console.log('You: ' + playerScore + '|' + 'Computer:' + computerScore );
// };
function gameScore(playerChoice){
const computerSelection = getComputerChoice();
let winnerLoser = playRound(playerChoice, computerSelection);
if (outcome[winnerLoser] === 1){
playerScore += 1;
} if (outcome[winnerLoser] === 0){
computerScore += 1;
} if(outcome[winnerLoser] === 2){
drawScore += 1;
}
};
// combining click and calling both PlayRound and game function for each choice
document.getElementById('paper').addEventListener('click', () => {
const playerChoice = 'paper';
const computerChoice = getComputerChoice();
const result = playRound(playerChoice, computerChoice);
gameScore(playerChoice);
if (result === 'player') {
playerScore++;
} else if (result === 'computer') {
computerScore++;
} else if (result === 'draw') {
drawGame++;
}
document.getElementById('player-score').textContent = playerScore;
document.getElementById('computer-score').textContent = computerScore;
document.getElementById('draw-score').textContent = drawScore;
});
document.getElementById('scissor').addEventListener('click', () => {
const playerChoice = 'scissor';
const computerChoice = getComputerChoice();
const result = playRound(playerChoice, computerChoice);
gameScore(playerChoice);
if (result === 'player') {
playerScore++;
} else if (result === 'computer') {
computerScore++;
} else if (result === 'draw') {
drawGame++;
}
document.getElementById('player-score').textContent = playerScore;
document.getElementById('computer-score').textContent = computerScore;
document.getElementById('draw-score').textContent = drawScore;
});
document.getElementById('rock').addEventListener('click', () => {
const playerChoice = 'rock';
const computerChoice = getComputerChoice();
const result = playRound(playerChoice, computerChoice);
gameScore(playerChoice);
if (result === 'player') {
playerScore++;
} else if (result === 'computer') {
computerScore++;
} else if (result === 'draw') {
drawGame++;
}
document.getElementById('player-score').textContent = playerScore;
document.getElementById('computer-score').textContent = computerScore;
document.getElementById('draw-score').textContent = drawScore;
});
// Get all score-container elements
const scoreContainers = document.querySelectorAll('.score-container');
// Set the display style of score-container to 'inline-block'
scoreContainers.forEach((container) => {
container.style.display = 'inline-block';
});