-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
300 lines (261 loc) · 8.5 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Classic Cookie Challenge #1</title>
<style>
body {
background-color: #222;
color: #ebcd23;
text-align: center;
padding: 100px 0;
font-family: monospace;
width: auto;
}
#input-container {
color: #222;
}
h1 {
text-decoration: underline;
}
#main-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
padding: 40px;
margin: 0 auto;
background-color: #ebcd23;
color: #eee;
max-width: 500px;
min-height: 400px;
width: auto;
height: auto;
transition: box-shadow .3s ease;
}
#main-container:hover {
box-shadow: 0 0 20px 0 #222 inset;
}
input, button {
background-color: #222;
color: #eee;
border: none;
padding: 5px;
border-radius: 3px;
transition: box-shadow .3s ease;
margin: 3px;
}
input {
width: 40px;
}
input:hover, button:hover {
box-shadow: 0 0 4px 0 #eee inset;
}
input:active, button:active {
box-shadow: 0 0 20px 0 #fff inset;
}
input:focus {
box-shadow: 0 0 10px #fff inset;
}
#grid-container {
background-color: #e6ce46;
margin: 20px auto;
min-width: 300px;
min-height: 200px;
width: auto;
height: auto;
border: 3px dashed #9c390baa;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
.row {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-evenly;
width: 100%;
}
.cell {
padding: 3px 10px;
margin: 3px;
background-color: #e6da9a;
color: #222;
transition: background-color .3s ease, color .3s ease;
}
#error {
color: #d22;
font-size: 15px;
}
#result {
color: #2a3;
font-size: 15px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>Classic Cookie Challenge #1</h1>
<h2>-Michael Woodruff</h2>
<div id="main-container">
<div id="input-container">
<input type="text" placeholder="width" maxlength="2" id="width">
X
<input type="text" placeholder="height" maxlength="2" id="height">
</div>
<div id="button-container">
<button id="reset">Reset</button>
<button id="cookiefy">Cookiefy!</button>
</div>
<div id="grid-container">
<!-- dynamic cookie grid here :] -->
</div>
<button id="calculate">Calculate Lines</button>
</div>
<p id="error"></p>
<p id="result"></p>
<script>
// helping global variables
var width;
var height;
var cookieGrid;
var result;
var newLength;
// utility functions to clear output
let clearError = () => {
$('#error').empty();
};
let clearResult = () => {
$('#result').empty();
};
let clearOutput = () => {
clearError();
clearResult();
};
// creates cookie grid based on width and height input
$('#cookiefy').click(() => {
clearOutput();
// retrieve width and height
let tempWidth = $('#width').val();
let tempHeight = $('#height').val();
// error check
if (tempWidth === '' || tempHeight === '') {
$('#error').html('Error: Missing width or height');
return;
}
if (!jQuery.isNumeric(tempWidth) || !jQuery.isNumeric(tempHeight)) {
$('#error').html('Error: width and height must be numeric');
return;
}
if (tempWidth <= 0 || tempHeight <= 0) {
$('#error').html('Error: Width and height must be at least 1');
return;
}
if (tempWidth > 15 || tempHeight > 15) {
$('#error').html('Error: For the sake of sanity, keep the dimensions no more than 15');
return;
}
// store width and height
width = tempWidth;
height = tempHeight;
// fill in grid with default values
$('#grid-container').empty();
let newHTML = '';
for (let i = 0; i < height; i++) {
newHTML += '<div class="row">';
for (let j = 0; j < width; j++) {
newHTML += '<button class="cell">0</button>';
}
newHTML += '</div>';
}
$('#grid-container').html(newHTML);
// functionality of cell buttons
$('.cell').on('click', (e) => {
clearOutput();
let cell = e.currentTarget;
if (cell.innerHTML === '0') {
cell.innerHTML = '1';
cell.style.backgroundColor = '#2e1a03';
cell.style.color = "#e6da9a";
}
else {
cell.innerHTML = '0';
cell.style.backgroundColor = '#e6da9a';
cell.style.color = "#222";
}
});
});
// resets grid to default values
$('#reset').click(() => {
$('#grid-container').children().toArray().forEach(row => {
row.childNodes.forEach(cell => {
cell.innerHTML = '0';
cell.style.backgroundColor = '#e6da9a';
cell.style.color = "#222";
});
});
clearOutput();
});
// recursive function for calculating line length
let findLine = (row, col) => {
newLength++;
cookieGrid[row][col] = '2';
//above
if (row > 0 && cookieGrid[row - 1][col] === '1') {
findLine(row - 1, col);
}
//right
if (col < width - 1 && cookieGrid[row][col + 1] === '1') {
findLine(row, col + 1);
}
//below
if (row < height - 1 && cookieGrid[row + 1][col] === '1') {
findLine(row + 1, col);
}
//left
if (col > 0 && cookieGrid[row][col - 1] === '1') {
findLine(row, col - 1);
}
};
// calculates all line lengths and adds to result
$('#calculate').click(() => {
clearOutput();
// retrive rows and error check
let rows = $('#grid-container').children().toArray();
if (rows.length === 0) {
$('#error').html('Error: That\'s not a cookie!');
return;
}
// populate cookie grid array (not the grid the user sees)
cookieGrid = [];
rows.forEach(row => {
let newGridRow = [];
row.childNodes.forEach(cell => {
newGridRow.push(cell.innerHTML);
});
cookieGrid.push(newGridRow);
});
// algorithm to find line lengths (uses recursive function findLine())
result = [];
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
if (cookieGrid[i][j] === '1') {
newLength = 0;
findLine(i, j);
result.push(newLength);
}
}
}
// output result
if (result.length === 0) {
$('#result').html('Result: none');
return;
}
$('#result').html(`Result: [${result.toString()}]`);
});
</script>
</body>
</html>