-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (131 loc) · 5.11 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Select DOM elements and define variables */
const colorSquares = document.querySelectorAll('.color-square');
const difficulties = document.querySelector('#difficulties');
const answerRGB = document.querySelector('.answerRGB');
const playAgain = document.querySelector('.play-again');
const guesses = document.querySelector('.guesses');
let numberOfGuesses = 2;
let answerSquare;
let answer;
let previousGuess;
const stats = {
'correct': 0,
'incorrect': 0,
'wins': 0,
'losses': 0
}
const wins = document.querySelector('#wins');
const losses = document.querySelector('#losses');
const percentage = document.querySelector('#percentage');
/* Define functions */
// Returns a random background in 'rgb(255, 150, 147)' format
function randomBackgroundRGB() {
colors = [];
for (let i = 0; i < 3; i++) {
colors.push(Math.floor(Math.random() * 256));
}
return `rgb(${colors.join(', ')})`;
}
function resetGame() {
// Remove any previous style
if (answerSquare) {
answerSquare.textContent = '';
answerSquare.style.transform = '';
}
// Reset the colors on each square
colorSquares.forEach(square => {
const color = randomBackgroundRGB();
square.style.background = color;
square.dataset.color = color;
})
// Randomly set the answer to one of the squares
const index = Math.floor((Math.random() * colorSquares.length));
answer = colorSquares[index].dataset.color;
answerSquare = colorSquares[index]; // used for displaying the correct square if you lose the game
// Update text
answerRGB.textContent = answer.toUpperCase();
// Update numberOfGuesses
const localGuesses = window.localStorage.getItem('guesses');
numberOfGuesses = localGuesses ? localGuesses : 2;
guesses.textContent = `${numberOfGuesses} ${numberOfGuesses > 1 ? 'guesses' : 'guess'} remaining`;
// Set difficulty style based on numberOfGuesses
const selectedDifficulty = document.querySelector(`li[data-guesses='${numberOfGuesses}']`);
selectedDifficulty.style.textDecoration = 'underline #FFF';
// Reset previousGuess
previousGuess = null;
// Testing that highlighted Text has something to do with the color
//colorSquares.forEach(color => color.style.background = 'yellow');
// wasn't related to the color
}
function checkGuess() {
if (numberOfGuesses === 0) return;
if (previousGuess && previousGuess == this) return;
const guess = this.dataset.color;
//console.log(guess);
// win game
if (guess === answer) {
displayTextInSquare(answerSquare, 'You win!');
// Update States
stats.correct ++;
stats.wins ++;
updateStats();
// Set guess to 0 to end the game
numberOfGuesses = 0;
return;
}
// incorrect guess
numberOfGuesses --;
guesses.textContent = `${numberOfGuesses} ${numberOfGuesses > 1 ? 'guesses' : 'guess'} remaining`;
stats.incorrect ++;
this.style.background = 'rgba(0, 0, 0, 0)'; // fade out square
previousGuess = this;
// lose game
if (numberOfGuesses === 0) {
stats.losses ++;
// Show correct answer
displayTextInSquare(answerSquare, 'Try Again!');
}
// update stats
updateStats();
}
// Takes a string in 'rgb(255, 150, 147)' fromat and returns its complimentary color
function rgbColorOffset(rgbString, offset) {
const matches = rgbString.match(/^rgb\((\d+),\s(\d+),\s(\d+)/);
const colors = [matches[1], matches[2], matches[3]];
return `rgb(${colors.map(color => (parseInt(color) - 255) * -1).join(',')})`;
}
function hoverSquare() {
this.classList.add('hover');
//console.log(this.dataset.color);
}
function removeHoverSquare() {
this.classList.remove('hover');
}
// Scales a square and adds .textContent in the square's complimentary color
function displayTextInSquare(square, text) {
square.style.transform = 'scale(1.2)';
square.style.color = rgbColorOffset(square.style.background, 255);
square.textContent = text;
}
function setDifficulty(e) {
if (e.target.dataset.guesses) {
window.localStorage.setItem('guesses', e.target.dataset.guesses);
// Remove underline styling from all children
difficulties.querySelectorAll('li').forEach(li => li.style.textDecoration = '');
// Add underline styling to target
e.target.style.textDecoration = 'underline #FFF';
resetGame();
}
}
function updateStats() {
wins.textContent = `Wins: ${stats.wins}`;
losses.textContent = `Losses: ${stats.losses}`;
percentage.textContent = `Guess Percentage: ${Math.round(stats.correct / (stats.correct + stats.incorrect)*100)}%`;
}
/* Set up Event Listeners */
window.addEventListener('load', resetGame);
playAgain.addEventListener('click', resetGame);
difficulties.addEventListener('click', setDifficulty);
colorSquares.forEach(square => square.addEventListener('mouseover', hoverSquare));
colorSquares.forEach(square => square.addEventListener('mouseleave', removeHoverSquare));
colorSquares.forEach(square => square.addEventListener('click', checkGuess));