-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.js
235 lines (158 loc) · 5.27 KB
/
wordle.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
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
// Step 1
/* create a table with 6 rows and 5 columns */
var table = document.createElement('table');
table.setAttribute('border','1');
for (var i = 0; i < 6; i++) {
var row = document.createElement('tr');
for (var j = 0; j < 5; j++) {
var cell = document.createElement('td');
cell.innerHTML = '<b>' + (i * 5 + j) + '</b>';
row.appendChild(cell);
}
table.appendChild(row);
}
document.body.appendChild(table);
// Step 2
/* Cells are grey, squared of 80px 80px */
var cells = table.getElementsByTagName('td');
for (var i = 0; i < cells.length; i++) {
cells[i].style.backgroundColor = '#ccc';
cells[i].style.width = '80px';
cells[i].style.height = '80px';
}
// Step 3
/* cells are half size */
for (var i = 0; i < cells.length; i++) {
cells[i].style.width = '40px';
cells[i].style.height = '40px';
}
// Step 4
/* there are no borders */
table.style.border = 'none';
// Step 5
/* Cells have no border */
for (var i = 0; i < cells.length; i++) {
cells[i].style.border = 'none';
}
// Step 6
/* Text is centered */
for (var i = 0; i < cells.length; i++) {
cells[i].style.textAlign = 'center';
}
// Step 7
/* text is white */
for (var i = 0; i < cells.length; i++) {
cells[i].style.color = 'white';
}
// Step 8
/* text is font helvetica and bold */
for (var i = 0; i < cells.length; i++) {
cells[i].style.fontFamily = 'Helvetica';
cells[i].style.fontWeight = 'bold';
}
// Step 9
/* cells have no padding */
for (var i = 0; i < cells.length; i++) {
cells[i].style.padding = '0px';
}
// Step 10
/* cells are empty */
for (var i = 0; i < cells.length; i++) {
cells[i].innerHTML = '';
}
// Step 11
/* add an input text field under the table */
var input = document.createElement('input');
input.setAttribute('type','text');
input.setAttribute('placeholder','Write Something');
document.body.appendChild(input);
// Step 12
/* add margin reduce text field width to 100px */
input.style.margin = '10px';
input.style.width = '100px';
// Step 13
/*add a button by the field with 'tick' character. Button must have un id named 'validate' */
var button = document.createElement('button');
button.innerHTML = '✓';
button.id = 'validate';
document.body.appendChild(button);
//Step14
/* add another button with id 'remove' and the 'x' character as label */
var button = document.createElement('button');
button.innerHTML = '✗';
button.id = 'remove';
document.body.appendChild(button);
// Step 15
/* center box text */
input.style.textAlign = 'center';
// Step 16
/* we cannot write more than 5 characters in the input field */
input.setAttribute('maxlength','5');
// Step 17
/* create variable named 'rowindex' starting at 0 */
var rowIndex = 0;
// Step 18
/* when entering text in the box, convert it to uppercase */
input.addEventListener('keyup', function(event) {
event.target.value = event.target.value.toUpperCase();
});
// Step 20
/* after writing on the text field, the value from every character will be writen on each cell of the selected row */
input.addEventListener('keyup', function(event) {
var cells = document.querySelectorAll('td');
for (var i = 0; i < cells.length; i++) {
if (i >= rowIndex * 5 && i < (rowIndex + 1) * 5) {
cells[i].innerHTML = input.value.charAt(i - rowIndex * 5);
}
}
});
// Step 21
/* store in a variable the secret word 'PAPER' */
var secretWord = 'PAPER';
// Step 22
/* When clicking validate button, we need to check if the characters of the chosen row are included in the secret word. If they are, the cell should be marked as yellow (#edc953) */
document.getElementById('validate').addEventListener('click', function(event) {
var cells = document.querySelectorAll('td');
for (var i = 0; i < cells.length; i++) {
if (i >= rowIndex * 5 && i < (rowIndex + 1) * 5) {
if (secretWord.indexOf(cells[i].innerHTML) >= 0) {
cells[i].style.backgroundColor = '#edc953';
}
}
}
});
// Step 23
/* if character is in right position, cell should be in green (#aedb95) */
document.getElementById('validate').addEventListener('click', function(event) {
for (var i = 0; i < cells.length; i++) {
if (i >= rowIndex * 5 && i < (rowIndex + 1) * 5) {
if (secretWord.charAt(i - rowIndex * 5) === cells[i].innerHTML) {
cells[i].style.backgroundColor = '#aedb95';
}
}
}
});
// Step 24
/* when clicking validate button, rowIndex variable increases */
document.getElementById('validate').addEventListener('click', function(event) {
rowIndex++;
});
// Step 25
/* when pushing remove button, every letter is erased, rowIndex goes to 0 and all cells turn gray */
document.getElementById('remove').addEventListener('click', function(event) {
var cells = document.querySelectorAll('td');
for (var i = 0; i < cells.length; i++) {
cells[i].innerHTML = '';
cells[i].style.backgroundColor = '#ccc';
}
rowIndex = 0;
});
// Step 26
/* save a collection of the following words: tools, super, faker, catch, cried in uppercase */
var words = ['TOOLS', 'SUPER', 'FAKER', 'CATCH', 'CRIED'];
// Step 27
/* when pressing remove, chose randomly the secret word from the words collection */
document.getElementById('remove').addEventListener('click', function(event) {
var randomIndex = Math.floor(Math.random() * words.length);
secretWord = words[randomIndex];
});