-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
104 lines (95 loc) · 4.21 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
102
103
104
//define function that will randomly return ‘Rock’, ‘Paper’ or ‘Scissors’
function computerPlay() {
//initialise result
let result;
//random number between 0 and 1
let randNum = Math.random();
//define conditions for returning ‘Rock’, ‘Paper’ or ‘Scissors’
if (randNum >= 0.666) {
return result = 'Rock';
} else if (randNum < 0.333) {
return result = 'Paper';
} else {
return result = 'Scissors';
}
}
// Function to refresh page
function refreshPage() {
window.location.reload(true);
}
//define function that takes the player's choice and compares it to the computer
function playRound(event) {
//ask for player input
//playerSelection = prompt('Rock, Paper or Scissors? Make a choice :)').toLowerCase();
//playerSelection = playSelect().toLowerCase;
//const playerSelection = () => {
if (event.target.id == 'rock' ||
event.target.id == 'scissors' ||
event.target.id == 'paper') {
const selection = event.srcElement.id //get the id of the click target
//console.log(selection);
//ask for player input
playerSelection = selection
}
// get the computer's choice
computerSelection = computerPlay().toLowerCase();
// select results div to append reset button at a later stage
const results = document.querySelector('.results');
// select result div to log the rounds
const result = document.querySelector('.result');
// initialize playerScore and computerScore
const playerScore = document.querySelector('.playerScore')
const computerScore = document.querySelector('.computerScore')
if (playerScore.textContent < 5 && computerScore.textContent < 5) {
//declare a winner
if (playerSelection === computerSelection) {
message = `${playerSelection} vs. ${computerSelection} ---> It's a tie :|`;
//console.log(message);
// append results div with current result
result.textContent = message
//return message;
} else if (playerSelection == 'rock' && computerSelection == 'scissors' ||
playerSelection == 'paper' && computerSelection == 'rock' ||
playerSelection == 'scissors' && computerSelection == 'paper') {
message = `${playerSelection} vs. ${computerSelection} ---> You won this round :)`;
//console.log(message);
// append results div with current result
result.textContent = message
// change playerScore
playerScore.textContent = parseInt(playerScore.textContent) + 1
//return message;
if (playerScore.textContent == 5) {
// Creating a button to refresh page
const btn = document.createElement('button');
btn.classList.add('reset');
btn.textContent = 'Play again!';
results.appendChild(btn);
// remove event listener to ignore additional clicks
window.removeEventListener('click', playRound);
// refresh page button at the end of the game
btn.addEventListener('click', refreshPage);
}
} else {
message = `${playerSelection} vs. ${computerSelection} ---> You lost this round :(`;
//console.log(message);
// append results div with current result
result.textContent = message
// change computerScore
computerScore.textContent = parseInt(computerScore.textContent) + 1
//return message;
if (computerScore.textContent == 5) {
// Creating a button to refresh page
const btn = document.createElement('button');
btn.classList.add('reset');
btn.textContent = 'Play again!';
results.appendChild(btn);
// remove event listener to ignore additional clicks
window.removeEventListener('click', playRound);
// refresh page button at the end of the game
btn.addEventListener('click', refreshPage);
}
}
}
}
// click event listener to play the game
window.addEventListener('click', playRound)