-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.html
430 lines (412 loc) · 19 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
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
425
426
427
428
429
430
<!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 Integers {
// 🧠 Constructor takes **any** number of arguments, which are integers
// 🧠 Instances have an `ints` property which is an array holding the integers above in the same order
// 🧠 Implement a `getAll` method which returns the data held in the `ints` property
// 🧠 Implement a `getEvens` method which returns an array with the integers that are even
// 🧠 Implement a `getNegatives` method which returns an array with the integers that negative
// 🧠 Implement a `getAllSquared` method which returns an array with the integers squared
// 🧠 Implement a `pop` method which removes the last integer from `ints` and returns it
// 🧠 Implement a `push` method which takes an int and pushes it to the end of `ints`, and returns the updated `ints` property
// 🧠 Implement a `getEveryNth` method which takes an int `n` and returns an array with the ints at position 0, n, n + n, n + n + n, etc
// 🧠 Implement a `getAllReversed` method which returns the ints in reverse order
// ❗ WARNING: except for `getAllReversed`, methods must respect the order of the integers
// ❗ WARNING: note that except for push and pop, the methods don't change the `ints` property
}
// 👉 CHALLENGE 2 NerfGun
class NerfGun {
// 🧠 Constructor takes one argument: the total number of projectiles supported by the instance
// 🧠 New instances initialize fully loaded and with the safety on
// 🧠 Instances have a `shoot` method that takes no arguments:
// * If there is ammo left and safety is off, method consumes one projectile and returns the string "Bang!"
// * Shooting on empty or with the safety on, returns the string "Click!"
// 🧠 Instances have a `reload` method that takes a number and tops up the instance by that number
// * Reloading returns a string in the following format: "<number> projectiles left!"
// * Reloading by more than capacity fills the instance to capacity
// * Passing an argument which is not a number larger than zero returns the string "Error reloading!"
// 🧠 Instances have a `toggleSafety` method that takes no arguments and switches the state of the safety
// * When engaging the safety, return "Safety enabled!"
// * When disengaging the safety, return "Safety disabled!"
// 🌟 HINT: instances need to keep track of their maximum capacity as well as their current capacity
}
// 👉 CHALLENGE 3 EBook
class EBook {
// 🧠 Constructor takes two arguments:
// * First arg is a string with the whole text of the book
// * Second arg is the number of characters contained in a single page of the book
// 🧠 Instances have a `next` method:
// * It takes no args and returns the next page of the book
// * If the next page is empty because we have reached the end, return null instead
// 🧠 Example of usage:
// const ebook = new EBook('abcdefg', 3)
// ebook.next() // returns "abc"
// ebook.next() // returns "def"
// ebook.next() // returns "g"
// ebook.next() // returns null
// 🌟 HINT: remember about the `slice` method which strings and arrays have
// ❗ Feel free to implement `back`, although this is not required nor graded
}
// 👉 CHALLENGE 4 AntFarm
class AntFarm {
// 🧠 Constructor takes **any** number of arguments, which are strings: the names of your ants
// 🧠 Instances of AntFarm have an `ants` property holding an array of ants:
// * Each ant is an object with a `name` prop and a `health` prop that starts at 100
// 🧠 Instances of AntFarm have a `work` method:
// * When invoked your ants do chores at a cost of 20 health points each
// * The method takes no arguments and returns the string "<ant number> ants starting work!"
// * If the `health` of any ant decreases to zero or below, it dies and is removed from `ants`
// * If `ants` contains no ants, the method returns "No ants here. Did you work them to death?"
// 🧠 Instances of AntFarm have a `feed` method:
// * The method takes as its only argument the name of the particular ant(s) to feed
// * When invoked, the ant(s) by the given name obtain a health increase of 15 points
// * The method returns the string "Yum!" if any ants by the given name exist
// * The method returns the string "<ant name> not found!" if no ants by the given name exist
// 🌟 Example:
// const farm = new AntFarm('Jess', 'Blake');
// farm.ants; // evaluates to [{name: 'Jess', health: 100}, {name: 'Blake', health: 100}]
// farm.work(); // returns "2 ants starting work!"
// farm.ants; // evaluates to [{name: 'Jess', health: 80}, {name: 'Blake', health: 80}]
// farm.feed('Jess'); // returns "Yum!"
// farm.ants; // evaluates to [{name: 'Jess', health: 95}, {name: 'Blake', health: 80}]
// 👉 HINT: when replacing a list with a modified list of the same length you can use map:
// let list = ['a','b','c']
// list = list.map(item => (item === "a" ? item + item : item))
// console.log(list) // prints ["aa","b","c"]
// 👉 HINT: when replacing a list with a filtered list you can use filter:
// let list = ['a','b','c']
// list = list.filter(item => item === "a")
// console.log(list) // prints ["a"]
// 👉 HINT: if you are not feeling confident with map and filter, solve the problem with for loops,
// and with the piece of mind of all tests passing, refactor to use map/filter
}
// 🧪 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>