generated from bloominstituteoftechnology/W_S3_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Untitled-1.html
424 lines (399 loc) · 15.3 KB
/
Untitled-1.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Sprint 3 Challenge</title>
<style>
* {
font-family: 'Times New Roman', Times, serif
}
.widget {
padding: 0 0 0.5rem 0.65rem;
margin-bottom: 0.5rem;
border: 1px solid black;
border-radius: 0.5rem;
}
.widget p {
font-style: italic;
}
</style>
</head>
<body>
<h1>Web Sprint 3 Challenge </h1>
<p>❗ See the last script tag for instructions on completing your Challenge</p>
<!-- widgets start, do not modify -->
<section class="widget">
<p>This widget uses the EBook class</p>
<textarea placeholder="Write your book"></textarea><br />
<input type="number" placeholder="Characters per page" /><br /><br />
<button id="turnPage">Turn Page</button>
<p id="bookPage"></p>
</section>
<!-- widgets end -->
<!-- The first script tag loads a library called lodash that helps with testing -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<script id="challenge">
// In CHALLENGES 1-4 you will write JavaScript classes
// ❗ Do not rename the classes provided
// ❗ Do not alter the position of the script tags
// ❗ Debug properly using the Console
// 👉 CHALLENGE 1 - Integers
class PeterPan {
constructor(name) {
this.name = name;
this.age = 0;
}
ageGracefully() {
if (this.age < 12) {
this.age += 1;
}
}
}
// 👉 CHALLENGE 2
class AirPlane {
constructor(model) {
this.model = model;
this.altitude = 0;
}
up(meters) {
this.altitude += meters;
return this.altitude;
}
down(meters) {
if (this.altitude - meters < 0) {
this.altitude = 0;
return "CRASH!";
} else {
this.altitude -= meters;
return this.altitude;
}
}
}
// 👉 CHALLENGE 3
class Bulb {
constructor() {
this.isOn = false; // Initial value of isOn is false
}
turnOn() {
this.isOn = true;
return "You turned the bulb ON";
}
turnOff() {
this.isOn = false;
return "You turned the bulb OFF";
}
toggle() {
this.isOn = !this.isOn;
return this.isOn ? "You turned the bulb ON" : "You turned the bulb OFF";
}
}
// 👉 CHALLENGE 4
class CountDown {
constructor(start) {
this.current = start; // Initialize the countdown starting number
}
count() {
if (this.current > 0) {
const result = `${this.current}!`; // Return the current number with "!"
this.current--; // Decrement the current count
return result;
} else if (this.current === 0) {
this.current--; // Prepare for the next call after liftoff
return "Liftoff!"; // Return "Liftoff!" when reaching zero
} else {
return null; // Return null after liftoff
}
}
}
// 🧪 TESTS, do not make any changes below this line ===================
// 🧪 TESTS, do not make any changes below this line ===================
// 🧪 TESTS, do not make any changes below this line ===================
globalThis.challengeVersion = 1
globalThis.Integers = Integers
globalThis.NerfGun = NerfGun
globalThis.EBook = EBook
globalThis.AntFarm = AntFarm
try {
testClass('CHALLENGE 1 - class Integers', [
function () {
let actual, description = 'getAll returns all the ints'
let expected = [1, 2, 3, 4]
try {
const ints = new Integers(1, 2, 3, 4)
actual = ints.getAll()
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'getEvens returns the even ints'
let expected = [0, 2, 4]
try {
const ints = new Integers(0, 2, 3, 5, 4)
actual = ints.getEvens()
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'getNegatives returns the negative ints'
let expected = [-3, -5]
try {
const ints = new Integers(-3, 5, -5, 0)
actual = ints.getNegatives()
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'getAllSquared returns the ints squared'
let expected = [4, 1, 0, 36, 25]
try {
const ints = new Integers(2, 1, 0, 6, 5)
actual = ints.getAllSquared()
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'pop removes the last int and returns it'
let expected = [3, [1, 2]]
try {
const ints = new Integers(1, 2, 3)
actual = []
actual.push(ints.pop())
actual.push(ints.ints)
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'push adds an integer into ints and returns the updated ints'
let expected = [[1, 2, 3, 4], [1, 2, 3, 4]]
try {
const ints = new Integers(1, 2, 3)
actual = []
actual.push(ints.push(4))
actual.push(ints.ints)
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'getEveryNth returns ints at every nth index'
let expected = [[1, 3, 5], [1, 4]]
try {
const ints = new Integers(1, 2, 3, 4, 5, 6)
actual = []
actual.push(ints.getEveryNth(2))
actual.push(ints.getEveryNth(3))
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'getAllReversed returns the ints in reverse order'
let expected = [[6, 5, 4, 3, 2, 1], [0, 1, 9, 3, 6, 9]]
try {
const ints = new Integers(1, 2, 3, 4, 5, 6)
const ints2 = new Integers(9, 6, 3, 9, 1, 0)
actual = []
actual.push(ints.getAllReversed())
actual.push(ints2.getAllReversed())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
])
testClass('CHALLENGE 2 - class NerfGun', [
function () {
let actual, description = 'A new NerfGun has its safety on'
let expected = "Click!"
try {
const nerf = new NerfGun(6)
actual = nerf.shoot()
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'A NerfGun can disable safety, shoot, re-enable safety'
let expected = ["Safety disabled!", "Bang!", "Safety enabled!", "Click!"]
try {
const nerf = new NerfGun(6)
actual = []
actual.push(nerf.toggleSafety())
actual.push(nerf.shoot())
actual.push(nerf.toggleSafety())
actual.push(nerf.shoot())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'A NerfGun can not shoot after running out of projectiles'
let expected = ["Bang!", "Bang!", "Bang!", "Click!"]
try {
const nerf = new NerfGun(3)
nerf.toggleSafety()
actual = []
actual.push(nerf.shoot()); actual.push(nerf.shoot()); actual.push(nerf.shoot()); actual.push(nerf.shoot())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'A NerfGun can be reloaded to less than capacity'
let expected = ["2 projectiles left!", "Bang!", "Bang!", "Click!"]
try {
const nerf = new NerfGun(3)
nerf.toggleSafety()
nerf.shoot()
nerf.shoot()
nerf.shoot()
actual = []
actual.push(nerf.reload(2))
actual.push(nerf.shoot()); actual.push(nerf.shoot()); actual.push(nerf.shoot())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Reloading to more than capacity only fills NerfGun to capacity'
let expected = ["3 projectiles left!", "Bang!", "Bang!", "Bang!", "Click!"]
try {
const nerf = new NerfGun(3)
nerf.toggleSafety()
nerf.shoot()
actual = []
actual.push(nerf.reload(100))
actual.push(nerf.shoot()); actual.push(nerf.shoot())
actual.push(nerf.shoot()); actual.push(nerf.shoot())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Reloading NerfGun incorrectly yields an error message'
let expected = ["Error reloading!", "Error reloading!", "Error reloading!"]
try {
const nerf = new NerfGun(6)
actual = []
actual.push(nerf.reload(0))
actual.push(nerf.reload(-1))
actual.push(nerf.reload('5'))
} catch (err) { actual = err.message }
return [description, expected, actual]
},
])
testClass('CHALLENGE 3 - class EBook', [
function () {
let actual, description = 'Ebook can turn pages'
let expected = ['abc', 'def', 'g']
try {
const book = new EBook('abcdefg', 3)
actual = []
actual.push(book.next()); actual.push(book.next()); actual.push(book.next())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Turning page yields null when there is no more text'
let expected = ['abcd', 'efgh', 'ij', null, null]
try {
const book = new EBook('abcdefghij', 4)
actual = []
actual.push(book.next()); actual.push(book.next()); actual.push(book.next())
actual.push(book.next()); actual.push(book.next())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
])
testClass('CHALLENGE 4 - class AntFarm', [
function () {
let actual, description = 'AntFarm initializes the ants correctly'
let expected = [
{ name: 'Jess', health: 100 }, { name: 'Blake', health: 100 }, { name: 'Jess', health: 100 }
]
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
actual = farm.ants
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Working substracts 20 health points'
let expected = [
{ name: 'Jess', health: 80 }, { name: 'Blake', health: 80 }, { name: 'Jess', health: 80 }
]
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
farm.work()
actual = farm.ants
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Overworking kills the ants'
let expected = []
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
farm.work(); farm.work(); farm.work(); farm.work(); farm.work()
actual = farm.ants
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Working returns the correct string'
let expected = ["3 ants starting work!", "1 ants starting work!", "No ants here. Did you work them to death?"]
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
farm.ants[0].health++
actual = []
farm.work(); farm.work(); farm.work(); farm.work()
actual.push(farm.work()); actual.push(farm.work()); actual.push(farm.work())
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Feeding increases 15 health points of lucky ant(s)'
let expected = [{ name: "Jess", "health": 115 }, { name: "Blake", health: 100 }, { name: "Jess", health: 115 }]
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
farm.feed('Jess')
actual = farm.ants
} catch (err) { actual = err.message }
return [description, expected, actual]
},
function () {
let actual, description = 'Feeding returns the correct string'
let expected = ["Yum!", "bad name not found!", "another bad name not found!"]
try {
const farm = new AntFarm('Jess', 'Blake', 'Jess')
actual = []
actual.push(farm.feed('Blake'))
actual.push(farm.feed('bad name'))
actual.push(farm.feed('another bad name'))
} catch (err) { actual = err.message }
return [description, expected, actual]
},
])
function testClass(testTitle, tests) {
console.log(testTitle)
tests.forEach((test, idx) => {
const [, expected, actual] = test()
const [desJSON, expJSON, actJSON] = test().map(JSON.stringify)
if (_.isEqual(expected, actual)) {
console.log(`\t✅ Test ${idx + 1} ${desJSON} PASSED`)
} else {
console.log(`\t❌ Test ${idx + 1} ${desJSON} FAILED:
Expected ${expJSON}
Actual ${actJSON}`)
}
})
}
let getTextArea = () => document.querySelector('.widget textarea')
let getNumberInput = () => document.querySelector('.widget input[type=number]')
const getText = () => getTextArea().value
const getChars = () => Number(getNumberInput().value)
turnPage.disabled = true
let book
let updateBook = () => {
turnPage.disabled = (getText() && getChars() && getChars() > 0) ? false : true
book = new EBook(getText(), getChars())
}
getTextArea().oninput = updateBook
getNumberInput().oninput = updateBook
turnPage.onclick = () => {
bookPage.textContent = book.next() || 'The End'
}
} catch (err) { console.error(err.stack) }
</script>
</body>
</html>