generated from bloominstituteoftechnology/W_S2_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
273 lines (253 loc) · 9.98 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Sprint 2 Challenge</title>
<script id="challenge">
// 👉 CHALLENGE 1
function profileActivation(profile, reason) {
if (profile.active === true && reason === undefined) {
return 'confirm status manually'
}
if (profile.active === false && profile.reason !== undefined) {
profile.active = true;
delete profile.reason
} else if (profile.active === true) {
profile.active = false;
profile.reason = reason
}
return profile
}
// 👉 CHALLENGE 2
function mineSweeper(grid, x, y) {
const a = x - 1;
const b = y - 1;
for (let i = 0; i < grid.length; i++) {
for (let j = 1; j < grid[i].length; j++) {
if ((a < 0 || a > 2) || (b < 0 || b > 2)) {
return 'invalid coordinates'
} else if (grid[b][a].includes("🟦")) {
return "🟦 🥳"
} else {
return "🟥 💀"
}
}
}
}
// 👉 CHALLENGE 3
function booleanize(obj) {
const object = obj
let result
for (const [key, value] of Object.entries(object)) {
const keyName = key.length
if (value === null) {
delete object[key]
result = object
}
if (value === 1) {
object[key] = true
result = object
}
if (value === 0) {
object[key] = false
result = object
}
if (keyName > 9) {
result = "shorten all prop names to 9 chars or less"
}
}
return result
}
// 👉 CHALLENGE 4
function scrub(text, forbidden) {
//Creates an array of words from sentence provided by 'text'.
let textArray = String(text).split(' ')
let scrubbedText
//Checks if any word in textArray is forbidden.
if (textArray.includes(...forbidden)) {
//Iterates thru each word in the array and compares it to forbidden, isolating the forbidden words.
textArray.forEach(word => {
if(forbidden.includes(word)) {
//censors the forbidden word
let scrubbedWord = word.replace(/./g, 'x')
//replaces the old word for the new value
textArray[textArray.indexOf(word)] = scrubbedWord
//turns the array back into a string
scrubbedText = textArray.join(' ')
return scrubbedText
}
})
return scrubbedText
}
//If nothing is forbidden, simply return the original
else {
return text
}
}
// 👉 CHALLENGE 5 (bonus, NOT graded)
function normalizePhoneNumber(num) {
let configNum
let numArray = String(num).split('')
numArray.forEach(element => {
if (num.length = 10) {
let areaCode = `(` + `${numArray[0]}` + `${numArray[1]}` + `${numArray[2]}` + `) `
let threeDigit = `${numArray[3]}` + `${numArray[4]}` + `${numArray[5]}-`
let fourDigit = `${numArray[6]}` + `${numArray[7]}` + `${numArray[8]}` + `${numArray[9]}`
configNum = areaCode + threeDigit + fourDigit
}
});
return configNum
}
// 👉 CHALLENGE 6 (bonus, NOT graded)
function diceRolls() {
}
// 🧪 DO NOT make any changes below this line ===================
globalThis.challengeVersion = 1
globalThis.profileActivation = profileActivation
globalThis.mineSweeper = mineSweeper
globalThis.booleanize = booleanize
globalThis.scrub = scrub
</script>
<style>
.widget {
padding: 0 0 0.5rem 0.65rem;
margin-bottom: 0.5rem;
border: 1px solid black;
border-radius: 0.5rem;
}
.widget p {
font-size: 0.75rem;
font-style: italic;
}
.row>div {
display: inline-block;
background-color: lightgrey;
border: 1px solid grey;
width: 2rem;
height: 2rem;
cursor: pointer;
}
#outcome {
font-size: 3rem;
}
</style>
</head>
<body>
<h1>Web Sprint 2 Challenge </h1>
<p>Open this HTML document in VSCode to find your challenges</p>
<!-- widgets start -->
<section class="widget">
<p>Click on a square! (this widget uses the mineSweeper function)</p>
<div class="row" id="row1">
<div></div>
<div></div>
<div></div>
</div>
<div class="row" id="row2">
<div></div>
<div></div>
<div></div>
</div>
<div class="row" id="row3">
<div></div>
<div></div>
<div></div>
</div>
<span id="outcome"></span>
</section>
<form class="widget">
<p>Type a ten-digit number! (this widget uses the normalizePhoneNumber function)</p>
<input type="text" id="phoneNumInput" maxlength="10" />
<span id="normalized"></span>
</form>
<!-- widgets end -->
<!-- The following script loads from the Internet a library called Lodash that helps with testing -->
<script id="lodash" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<!-- The following script executes the tests you see when you open the Console in Chrome -->
<script id="tests">
runTests('CHALLENGE 1 - profileActivation', profileActivation, [
[[{ active: true }], 'confirm status manually'],
[[{ active: true }, 'because'], { active: false, reason: 'because' }],
[[{ active: false, reason: 'because' }], { active: true }],
])
runTests('CHALLENGE 2 - mineSweeper', mineSweeper, [
[[[["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]], 0, 4], "invalid coordinates"],
[[[["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]], 0, 1], "invalid coordinates"],
[[[["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟦", "🟦"]], 1, 4], "invalid coordinates"],
[[[["🟦", "🟦", "🟥"], ["🟦", "🟦", "🟦"], ["🟦", "🟦", "🟥"]], 1, 1], "🟦 🥳"],
[[[["🟦", "🟥", "🟦"], ["🟦", "🟦", "🟦"], ["🟥", "🟦", "🟥"]], 2, 1], "🟥 💀"],
[[[["🟥", "🟦", "🟥"], ["🟥", "🟥", "🟦"], ["🟥", "🟥", "🟦"]], 3, 1], "🟥 💀"],
[[[["🟥", "🟦", "🟦"], ["🟥", "🟦", "🟥"], ["🟦", "🟦", "🟦"]], 1, 2], "🟥 💀"],
[[[["🟥", "🟥", "🟥"], ["🟥", "🟦", "🟥"], ["🟦", "🟥", "🟦"]], 2, 2], "🟦 🥳"],
[[[["🟥", "🟦", "🟦"], ["🟦", "🟥", "🟥"], ["🟥", "🟥", "🟦"]], 3, 2], "🟥 💀"],
[[[["🟥", "🟥", "🟥"], ["🟦", "🟦", "🟥"], ["🟥", "🟥", "🟦"]], 1, 3], "🟥 💀"],
[[[["🟥", "🟦", "🟥"], ["🟥", "🟥", "🟥"], ["🟥", "🟦", "🟥"]], 2, 3], "🟦 🥳"],
[[[["🟥", "🟥", "🟥"], ["🟦", "🟥", "🟥"], ["🟥", "🟥", "🟦"]], 3, 3], "🟦 🥳"],
[[[["🟥", "🟦", "🟦"], ["🟥", "🟥", "🟦"], ["🟥", "🟥", "🟥"]], 1, 1], "🟥 💀"],
[[[["🟥", "🟦", "🟦"], ["🟥", "🟥", "🟦"], ["🟥", "🟥", "🟥"]], 2, 2], "🟥 💀"],
[[[["🟥", "🟦", "🟦"], ["🟥", "🟥", "🟦"], ["🟥", "🟥", "🟥"]], 3, 3], "🟥 💀"],
])
runTests('CHALLENGE 3 - booleanize', booleanize, [
[[{ bad1: null }], {}],
[[{ bad1: null, bad2: null }], {}],
[[{ '0123456789': 1 }], 'shorten all prop names to 9 chars or less'],
[[{ a: 1, b: 1 }], { a: true, b: true }],
[[{ a: 0, b: 0 }], { a: false, b: false }],
[[{ a: 1, b: 0, c: null, d: 'Lady Gaga' }], { a: true, b: false, d: 'Lady Gaga' }],
])
runTests('CHALLENGE 4 - scrub', scrub, [
[["out of the silent planet", ["of", "silent"]], "out xx the xxxxxx planet"],
[["out of the silent planet", ["of", "planet"]], "out xx the silent xxxxxx"],
[["the ghost of the navigator", ["the"]], "xxx ghost of xxx navigator"],
[["lost somewhere in time", []], "lost somewhere in time"],
[["aces high", ["high", "aces", "hearts"]], "xxxx xxxx"],
[["", ["high", "aces"]], ""],
])
runTests('CHALLENGE 5 (OPTIONAL) - normalizePhoneNumber', normalizePhoneNumber, [
[["1234567890"], "(123) 456-7890"],
[["1111111111"], "(111) 111-1111"],
[["9876543210"], "(987) 654-3210"],
])
console.log('\nCHALLENGE 6 (OPTIONAL) does not have auto tests - diceRolls')
function runTests(testName, func, tests) {
let results = []
tests.forEach(test => {
const originalArgsList = _.cloneDeep(test[0])
const argsList = test[0]
const expected = test[1]
const actual = func.apply(null, argsList)
results.push([argsList, expected, actual, originalArgsList])
})
console.log('\n' + testName)
if (results.every(result => _.isEqual(result[1], result[2]))) console.log('\t✅ All tests pass')
else if (results.every(result => !_.isEqual(result[1], result[2]))) console.log('\t❌ All tests fail')
else results.forEach((result, idx) => {
if (_.isEqual(result[1], result[2])) console.log(`\t✅ Test ${idx + 1} passes`)
else console.log(`\t❌ Test ${idx + 1} fails:
${func.name}(${result[3].map(JSON.stringify)})
👉 should return ${JSON.stringify(result[1])}
👉 but returns ${JSON.stringify(result[2])}`)
})
}
const gridElems = [Array.from(row1.children), Array.from(row2.children), Array.from(row3.children)]
const squares = ["🟥", "🟦"]
let grid = [[], [], []]
gridElems.forEach((row, idxRow) => {
row.forEach((square, idxSquare) => {
const emoji = squares[Math.floor(Math.random() * 2)]
grid[idxRow].push(emoji)
if (emoji === squares[0]) square.style.backgroundColor = '#ffecec'
else square.style.backgroundColor = '#f2f2ff'
square.onclick = () => {
const x = idxSquare + 1
const y = idxRow + 1
console.log(`\nYou clicked coordinates [${x}, ${y}]`)
outcome.textContent = mineSweeper(grid, x, y)
}
})
})
phoneNumInput.oninput = evt => {
normalized.textContent = normalizePhoneNumber(evt.target.value)
}
</script>
</body>
</html>