-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdata-structures.json
More file actions
3782 lines (3782 loc) · 246 KB
/
data-structures.json
File metadata and controls
3782 lines (3782 loc) · 246 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"name": "Data Structures",
"order": 2,
"time": "",
"helpRoom": "HelpJavaScript",
"challenges": [
{
"id": "587d8253367417b2b2512c6a",
"title": "Typed Arrays",
"description": [
"数组是一种 JavaScript 对象,它能存储各种数据类型的元素。",
"<code>var complexArr = [1, 5, \"2\", \"Word\", {\"name\": \"James\"}];</code>",
"通常浏览器会自动地为数组分配合适的内存空间,同时内存大小也会随着你增删元素而发生变化。",
"然而,在一些对高性能有要求以及有不同数据类型的情况下,有时需要更精确地给一个数组分配内存空间。",
"<dfn>Typed arrays</dfn> 就是解决这类问题的一种方案。 我们可以精确地给一个数组分配内存空间。如下列表说明了这种数组能够存放的数据类型以及不同数据类型所占据的字节数。",
"<table class='table table-striped'><tr><th>类型</th><th>元素所占字节数</th></tr><tr><td><code>Int8Array</code></td><td>1</td></tr><tr><td><code>Uint8Array</code></td><td>1</td></tr><tr><td><code>Uint8ClampedArray</code></td><td>1</td></tr><tr><td><code>Int16Array</code></td><td>2</td></tr><tr><td><code>Uint16Array</code></td><td>2</td></tr><tr><td><code>Int32Array</code></td><td>4</td></tr><tr><td><code>Uint32Array</code></td><td>4</td></tr><tr><td><code>Float32Array</code></td><td>4</td></tr><tr><td><code>Float64Array</code></td><td>8</td></tr></table>",
"有两种方式可以创这类数组。其中一种是直接创建,如下代码创建了一个长度为 3 的 <code>Int16Array</code>.",
"<blockquote>var i8 = new Int16Array(3);<br>console.log(i8);<br>// Returns [0, 0, 0]</blockquote>",
"我们也可以通过创建 <dfn>buffer</dfn> 的方式来决定一个数组要容纳多少元素。",
"<strong>注意</strong><br>我们可以通过分配上面列出的多个 bytes 的倍数来创建一个和上方代码中一样的类型数组。",
"<blockquote>// Create same Int16Array array differently<br>var byteSize = 6; // Needs to be multiple of 2<br>var buffer = new ArrayBuffer(byteSize);<br>var i8View = new Int16Array(buffer);<br>buffer.byteLength; // Returns 6<br>i8View.byteLength; // Returns 6<br>console.log(i8View); // Returns [0, 0, 0]</blockquote>",
" <dfn>Buffers</dfn> 是一类存储数据的特定类型对象。我们需要通过创建视图来访问这类对象,不能直接访问。",
"<blockquote>i8View[0] = 42;<br>console.log(i8View); // Returns [42, 0, 0]</blockquote>",
"<strong>注意</strong><br>Typed Arrays并没有像<code>.pop()</code>或<code>.push()</code>这些一般数组拥有的方法。 使用 <code>Array.isArray()</code> 方法对Typed Arrays做判断返回的是fail,而非true。因为更加简洁,所以更便于简化JavaScript引擎去实现这类数组。",
"<hr>",
"先创建一个64个字节的<code>buffer</code>,再创建一个类型是 <code>Int32Array</code> 名称叫做 <code>i32View</code>的视图。"
],
"tests": [
{
"text": " <code>buffer</code> 应该有64个字节的长度。",
"testString": "assert(buffer.byteLength === 64, 'Your <code>buffer</code> should be 64 bytes large.');"
},
{
"text": " <code>i32View</code> 视图应该有64个字节的长度。",
"testString": "assert(i32View.byteLength === 64, 'Your <code>i32View</code> view of your buffer should be 64 bytes large.');"
},
{
"text": " <code>i32View</code> 视图应该能容纳16个元素。",
"testString": "assert(i32View.length === 16, 'Your <code>i32View</code> view of your buffer should be 16 elements long.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"var buffer = new ArrayBuffer(64);\nvar i32View = new Int32Array(buffer);"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"var buffer;",
"var i32View;"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8250367417b2b2512c5e",
"title": "Learn how a Stack Works",
"description": [
"You are probably familiar with stack of books on your table. You have likely used the undo feature of a text editor. You are also probably used to hitting the back button on your phone to go back to the previous view in your app.",
"You know what they all have in common? They all store the data in a way so that you can traverse backwards.",
"The topmost book in the stack was the one that was put there last. If you remove that book from your stack's top, you would expose the book that was put there before the last book and so on.",
"If you think about it, in all the above examples, you are getting <dfn>Last-In-First-Out</dfn> type of service. We will try to mimic this with our code.",
"This data storage scheme is called a <dfn>Stack</dfn>. In particular, we would have to implement the <code>push()</code> method that pushes JavaScript objects at the top of the stack; and <code>pop()</code> method, that removes the JavaScript object that's at the top of the stack at the current moment.",
"<hr>",
"Here we have a stack of homework assignments represented as an array: <code>\"BIO12\"</code> is at the base, and <code>\"PSY44\"</code> is at the top of the stack.",
"Modify the given array and treat it like a <code>stack</code> using the JavaScript methods mentioned above. Remove the top element <code>\"PSY44\"</code> from the stack. Then add <code>\"CS50\"</code> to be the new top element of the stack."
],
"tests": [
{
"text": "<code>homeworkStack</code> should only contain 4 elements.",
"testString": "assert(homeworkStack.length === 4, '<code>homeworkStack</code> should only contain 4 elements.');"
},
{
"text": "The last element in <code>homeworkStack</code> should be <code>\"CS50\"</code>.",
"testString": "assert(homeworkStack[3] === 'CS50', 'The last element in <code>homeworkStack</code> should be <code>\"CS50\"</code>.');"
},
{
"text": "<code>homeworkStack</code> should not contain <code>\"PSY44\"</code>.",
"testString": "assert(homeworkStack.indexOf('PSY44') === -1, '<code>homeworkStack</code> should not contain <code>\"PSY44\"</code>.');"
},
{
"text": "The initial declaration of the <code>homeworkStack</code> should not be changed.",
"testString": "assert(code.match(/=/g).length === 1 && /homeworkStack\\s*=\\s*\\[\"BIO12\"\\s*,\\s*\"HIS80\"\\s*,\\s*\"MAT122\"\\s*,\\s*\"PSY44\"\\]/.test(code), 'The initial declaration of the <code>homeworkStack</code> should not be changed.');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"var homeworkStack = [\"BIO12\",\"HIS80\",\"MAT122\",\"PSY44\"];",
"// Only change code below this line",
""
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8250367417b2b2512c5f",
"title": "Create a Stack Class",
"description": [
"In the last section, we talked about what a stack is and how we can use an array to represent a stack. In this section, we will be creating our own stack class.",
"Although you can use arrays to create stacks, sometimes it is best to limit the amount of control we have with our stacks.",
"Apart from the <code>push</code> and <code>pop</code> method, stacks have other useful methods. Let's add a <code>peek</code>, <code>isEmpty</code>, and <code>clear</code> method to our stack class.",
"Instructions",
"Write a <code>push</code> method that pushes an element to the top of the stack, a <code>pop</code> method that removes the element on the top of the stack, a <code>peek</code> method that looks at the first element in the stack, an <code>isEmpty</code> method that checks if the stack is empty, and a <code>clear</code> method that removes all elements from the stack.",
"Normally stacks don't have this, but we've added a <code>print</code> helper method that console logs the collection."
],
"tests": [
{
"text": "Your <code>Stack</code> class should have a <code>push</code> method.",
"testString": "assert((function(){var test = new Stack(); return (typeof test.push === 'function')}()), 'Your <code>Stack</code> class should have a <code>push</code> method.');"
},
{
"text": "Your <code>Stack</code> class should have a <code>pop</code> method.",
"testString": "assert((function(){var test = new Stack(); return (typeof test.pop === 'function')}()), 'Your <code>Stack</code> class should have a <code>pop</code> method.');"
},
{
"text": "Your <code>Stack</code> class should have a <code>peek</code> method.",
"testString": "assert((function(){var test = new Stack(); return (typeof test.peek === 'function')}()), 'Your <code>Stack</code> class should have a <code>peek</code> method.');"
},
{
"text": "Your <code>Stack</code> class should have a <code>isEmpty</code> method.",
"testString": "assert((function(){var test = new Stack(); return (typeof test.isEmpty === 'function')}()), 'Your <code>Stack</code> class should have a <code>isEmpty</code> method.');"
},
{
"text": "Your <code>Stack</code> class should have a <code>clear</code> method.",
"testString": "assert((function(){var test = new Stack(); return (typeof test.clear === 'function')}()), 'Your <code>Stack</code> class should have a <code>clear</code> method.');"
},
{
"text": "The <code>peek</code> method should return the top element of the stack",
"testString": "assert((function(){var test = new Stack(); test.push('CS50'); return (test.peek() === 'CS50')}()), 'The <code>peek</code> method should return the top element of the stack');"
},
{
"text": "The <code>pop</code> method should remove and return the top element of the stack",
"testString": "assert((function(){var test = new Stack(); test.push('CS50'); return (test.pop() === 'CS50');}()), 'The <code>pop</code> method should remove and return the top element of the stack');"
},
{
"text": "The <code>isEmpty</code> method should return true if a stack does not contain any elements",
"testString": "assert((function(){var test = new Stack(); return test.isEmpty()}()), 'The <code>isEmpty</code> method should return true if a stack does not contain any elements');"
},
{
"text": "The <code>clear</code> method should remove all element from the stack",
"testString": "assert((function(){var test = new Stack(); test.push('CS50'); test.clear(); return (test.isEmpty())}()), 'The <code>clear</code> method should remove all element from the stack');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Stack() { ",
" var collection = [];",
" this.print = function() {",
" console.log(collection);",
" };",
" // Only change code below this line",
"",
" // Only change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8250367417b2b2512c60",
"title": "Create a Queue Class",
"description": [
"Like stacks, queues are a collection of elements. But unlike stacks, queues follow the FIFO (First-In First-Out) principle. Elements added to a queue are pushed to the tail, or the end, of the queue, and only the element at the front of the queue is allowed to be removed.",
"We could use an array to represent a queue, but just like stacks, we want to limit the amount of control we have over our queues.",
"The two main methods of a queue class is the enqueue and the dequeue method. The enqueue method pushes an element to the tail of the queue, and the dequeue method removes and returns the element at the front of the queue. Other useful methods are the front, size, and isEmpty methods.",
"Instructions",
"Write an enqueue method that pushes an element to the tail of the queue, a dequeue method that removes and returns the front element, a front method that lets us see the front element, a size method that shows the length, and an isEmpty method to check if the queue is empty."
],
"tests": [
{
"text": "Your <code>Queue</code> class should have a <code>enqueue</code> method.",
"testString": "assert((function(){var test = new Queue(); return (typeof test.enqueue === 'function')}()), 'Your <code>Queue</code> class should have a <code>enqueue</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have a <code>dequeue</code> method.",
"testString": "assert((function(){var test = new Queue(); return (typeof test.dequeue === 'function')}()), 'Your <code>Queue</code> class should have a <code>dequeue</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have a <code>front</code> method.",
"testString": "assert((function(){var test = new Queue(); return (typeof test.front === 'function')}()), 'Your <code>Queue</code> class should have a <code>front</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have a <code>size</code> method.",
"testString": "assert((function(){var test = new Queue(); return (typeof test.size === 'function')}()), 'Your <code>Queue</code> class should have a <code>size</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have an <code>isEmpty</code> method.",
"testString": "assert((function(){var test = new Queue(); return (typeof test.isEmpty === 'function')}()), 'Your <code>Queue</code> class should have an <code>isEmpty</code> method.');"
},
{
"text": "The <code>dequeue</code> method should remove and return the front element of the queue",
"testString": "assert((function(){var test = new Queue(); test.enqueue('Smith'); return (test.dequeue() === 'Smith')}()), 'The <code>dequeue</code> method should remove and return the front element of the queue');"
},
{
"text": "The <code>front</code> method should return value of the front element of the queue",
"testString": "assert((function(){var test = new Queue(); test.enqueue('Smith'); test.enqueue('John'); return (test.front() === 'Smith')}()), 'The <code>front</code> method should return value of the front element of the queue');"
},
{
"text": "The <code>size</code> method should return the length of the queue",
"testString": "assert((function(){var test = new Queue(); test.enqueue('Smith'); return (test.size() === 1)}()), 'The <code>size</code> method should return the length of the queue');"
},
{
"text": "The <code>isEmpty</code> method should return <code>false</code> if there are elements in the queue",
"testString": "assert((function(){var test = new Queue(); test.enqueue('Smith'); return !(test.isEmpty())}()), 'The <code>isEmpty</code> method should return <code>false</code> if there are elements in the queue');"
}
],
"solutions": [],
"hints": [],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Queue () { ",
" var collection = [];",
" this.print = function() {",
" console.log(collection);",
" };",
" // Only change code below this line",
"",
" // Only change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8255367417b2b2512c74",
"title": "Create a Priority Queue Class",
"description": [
"In this challenge you will be creating a Priority Queue. A Priority Queue is a special type of Queue in which items may have additional information which specifies their priority. This could be simply represented with an integer. Item priority will override placement order in determining the sequence items are dequeued. If an item with a higher priority is enqueued after items with lower priority, the higher priority item will be dequeued before all the others.",
"For instance, let’s imagine we have a priority queue with three items:",
"<code>[[’kitten’, 2], [‘dog’, 2], [‘rabbit’, 2]]</code>",
"Here the second value (an integer) represents item priority. If we enqueue <code>[‘human’, 1]</code> with a priority of <code>1</code> (assuming lower priorities are given precedence) it would then be the first item to be dequeued. The collection would like this:",
"<code>[[‘human’, 1], [’kitten’, 2], [‘dog’, 2], [‘rabbit’, 2]]</code>.",
"We’ve started writing a <code>PriorityQueue</code> in the code editor. You will need to add an <code>enqueue</code> method for adding items with a priority, a <code>dequeue</code> method for removing items, a <code>size</code> method to return the number of items in the queue, a <code>front</code> method to return the element at the front of the queue, and finally an <code>isEmpty</code> method that will return <code>true</code> if the queue is empty or <code>false</code> if it is not.",
"The <code>enqueue</code> should accept items with the format shown above (<code>['human', 1]</code>) where <code>1</code> represents the priority. The <code>dequeue</code> should return only the current item, not its priority."
],
"tests": [
{
"text": "Your <code>Queue</code> class should have a <code>enqueue</code> method.",
"testString": "assert((function(){var test = new PriorityQueue(); return (typeof test.enqueue === 'function')}()), 'Your <code>Queue</code> class should have a <code>enqueue</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have a <code>dequeue</code> method.",
"testString": "assert((function(){var test = new PriorityQueue(); return (typeof test.dequeue === 'function')}()), 'Your <code>Queue</code> class should have a <code>dequeue</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have a <code>size</code> method.",
"testString": "assert((function(){var test = new PriorityQueue(); return (typeof test.size === 'function')}()), 'Your <code>Queue</code> class should have a <code>size</code> method.');"
},
{
"text": "Your <code>Queue</code> class should have an <code>isEmpty</code> method.",
"testString": "assert((function(){var test = new PriorityQueue(); return (typeof test.isEmpty === 'function')}()), 'Your <code>Queue</code> class should have an <code>isEmpty</code> method.');"
},
{
"text": "Your PriorityQueue should correctly keep track of the current number of items using the <code>size</code> method as items are enqueued and dequeued.",
"testString": "assert((function(){var test = new PriorityQueue(); test.enqueue(['David Brown', 2]); test.enqueue(['Jon Snow', 1]); var size1 = test.size(); test.dequeue(); var size2 = test.size(); test.enqueue(['A', 3]); test.enqueue(['B', 3]); test.enqueue(['C', 3]); return (size1 === 2 && size2 === 1 && test.size() === 4)}()), 'Your PriorityQueue should correctly keep track of the current number of items using the <code>size</code> method as items are enqueued and dequeued.');"
},
{
"text": "The <code>isEmpty</code> method should return <code>true</code> when the queue is empty.",
"testString": "assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 1]); test.enqueue(['B', 1]); test.dequeue(); var first = test.isEmpty(); test.dequeue(); return (!first && test.isEmpty()); }()), 'The <code>isEmpty</code> method should return <code>true</code> when the queue is empty.');"
},
{
"text": "The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.",
"testString": "assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 5]); test.enqueue(['B', 5]); test.enqueue(['C', 5]); test.enqueue(['D', 3]); test.enqueue(['E', 1]); test.enqueue(['F', 7]); var result = []; result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); return result.join('') === 'EDABCF';}()), 'The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function PriorityQueue () { \n this.collection = []; \n this.printCollection = function(){ \n console.log(this.collection); \n }; \n this.size = function() { \n return this.collection.length; \n }; \n this.isEmpty = function() { \n return this.size() > 0 ? false : true; \n }; \n this.enqueue = function (newitem) {\n if (this.isEmpty()) {\n return this.collection.push(newitem);\n }\n\n this.collection = this.collection.reverse();\n var found_index = this.collection.findIndex(function (item) {\n return newitem[1] >= item[1];\n });\n if (found_index === -1) {\n this.collection.push(newitem);\n } else {\n this.collection.splice(found_index, 0, newitem);\n }\n this.collection = this.collection.reverse();\n}; \n this.dequeue = function() { \n if (!this.isEmpty()) { \n return this.collection.shift()[0]; \n } else { \n return 'The queue is empty.' \n } \n }; \n }"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function PriorityQueue () {",
" this.collection = [];",
" this.printCollection = function() {",
" console.log(this.collection);",
" };",
" // Only change code below this line",
"",
" // Only change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8255367417b2b2512c75",
"title": "Create a Circular Queue",
"description": [
"In this challenge you will be creating a Circular Queue. A circular queue is basically a queue that writes to the end of a collection then begins over writing itself at the beginning of the collection. This is type of data structure has some useful applications in certain situations. For example, a circular queue can be used for streaming media. Once the queue is full, new media data simply begins to overwrite old data.",
"A good way to illustrate this concept is with an array:",
"<blockquote>[1, 2, 3, 4, 5]<br> ^Read @ 0<br> ^Write @ 0</blockquote>",
"Here the read and write are both at position <code>0</code>. Now the queue gets 3 new records <code>a</code>, <code>b</code>, and <code>c</code>. Our queue now looks like:",
"<blockquote>[a, b, c, 4, 5]<br> ^Read @ 0<br> ^Write @ 3</blockquote>",
"As the read head reads, it can remove values or keep them:",
"<blockquote>[null, null, null, 4, 5]<br> ^Read @ 3<br> ^Write @ 3</blockquote>",
"Once the write reaches the end of the array it loops back to the beginning:",
"<blockquote>[f, null, null, d, e]<br> ^Read @ 3<br> ^Write @ 1</blockquote>",
"This approach requires a constant amount of memory but allows files of a much larger size to be processed.",
"Instructions:",
"In this challenge we will implement a circular queue. The circular queue should provide <code>enqueue</code> and <code>dequeue</code> methods which allow you to read from and write to the queue. The class itself should also accept an integer which you can use to specify the size of the queue when you create it. We've written the starting version of this class for you in the code editor. When you enqueue items to the queue, the write pointer should advance forward and loop back to the beginning once it reaches the end of the queue. Likewise, the read pointer should advance forward as you dequeue items. The write pointer should not be allowed to move past the read pointer (our class won't let you overwrite data you haven't read yet) and the read pointer should not be able to advance past data you have written.",
"In addition, the <code>enqueue</code> method should return the item you enqueued if it is successfully and otherwise return <code>null</code>. Similarly, when you dequeue an item it should be returned and if you cannot dequeue you should return <code>null</code>."
],
"tests": [
{
"text": "The <code>enqueue</code> method adds items to the circular queue.",
"testString": "assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), 'The <code>enqueue</code> method adds items to the circular queue.');"
},
{
"text": "You cannot enqueue items past the read pointer.",
"testString": "assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); test.enqueue(13); test.enqueue(25); test.enqueue(59); var print = test.print(); return print[0] === 17 && print[1] === 32 && print[2] === 591; })(), 'You cannot enqueue items past the read pointer.');"
},
{
"text": "The <code>dequeue</code> method dequeues items from the queue.",
"testString": "assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591; })(), 'The <code>dequeue</code> method dequeues items from the queue.');"
},
{
"text": "After an item is dequeued its position in the queue should be reset to <code>null</code>.",
"testString": "assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(672); test.dequeue(); test.dequeue(); var print = test.print(); return print[0] === null && print[1] === null && print[2] === 672; })(), 'After an item is dequeued its position in the queue should be reset to <code>null</code>.');"
},
{
"text": "Trying to dequeue past the write pointer returns <code>null</code> and does not advance the write pointer.",
"testString": "assert((function(){ var test = new CircularQueue(3); test.enqueue(17); test.enqueue(32); test.enqueue(591); return test.dequeue() === 17 && test.dequeue() === 32 && test.dequeue() === 591 && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.dequeue() === null && test.enqueue(100) === 100 && test.dequeue() === 100; })(), 'Trying to dequeue past the write pointer returns <code>null</code> and does not advance the write pointer.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"class CircularQueue { \n constructor(size) { \n this.queue = []; \n this.read = 0; \n this.write = 0; \n this.max = size - 1; \n while (size > 0) { \n this.queue.push(null); \n size--; \n } \n } \n print() { \n return this.queue; \n } \n enqueue(item) { \n if (this.queue[this.write] === null) { \n this.queue[this.write] = item; \n if (this.write === this.max) { \n this.write = 0; \n } else { \n this.write++; \n } \n return item; \n } \n return null; \n } \n dequeue() { \n if (this.queue[this.read] !== null) { \n var item = this.queue[this.read]; \n this.queue[this.read] = null; \n if (this.read === this.max) { \n this.read = 0; \n } else { \n this.read++; \n } \n return item; \n } else { \n return null; \n } \n } \n }"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"class CircularQueue {",
" constructor(size) {",
"",
" this.queue = [];",
" this.read = 0;",
" this.write = 0;",
" this.max = size - 1;",
"",
" while (size > 0) {",
" this.queue.push(null);",
" size--;",
" }",
"",
" }",
"",
" print() {",
" return this.queue;",
" }",
"",
"",
" enqueue(item) {",
" // Only change code below this line",
"",
" // Only change code above this line",
" }",
"",
" dequeue() {",
" // Only change code below this line",
"",
" // Only change code above this line",
" }",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "8d1323c8c441eddfaeb5bdef",
"title": "Create a Set Class",
"description": [
"In the next few exercises we are going to create a function to emulate a data structure called a \"Set\". A Set is like an array, but it cannot contain duplicate values. The typical use for a Set is to simply check for the presence of an item. This can be implemented with an object, for instance:",
"<blockquote>var set = new Object();<br>set.foo = true;<br>// See if foo exists in our set:<br>console.log(set.foo) // true</blockquote>",
"In the next few exercises, we will build a full featured Set from scratch.",
"For this exercise, create a function that will add a value to our set collection as long as the value does not already exist in the set. For example:",
"<blockquote>this.add = function(element) {<br> //some code to add value to the set<br>}</blockquote>",
"The function should return <code>true</code> if the value is successfully added and <code>false</code> otherwise."
],
"tests": [
{
"text": "Your <code>Set</code> class should have an <code>add</code> method.",
"testString": "assert((function(){var test = new Set(); return (typeof test.add === 'function')}()), 'Your <code>Set</code> class should have an <code>add</code> method.');"
},
{
"text": "Your <code>add</code> method should not add duplicate values.",
"testString": "assert((function(){var test = new Set(); test.add('a'); test.add('b'); test.add('a'); var vals = test.values(); return (vals[0] === 'a' && vals[1] === 'b' && vals.length === 2)}()), 'Your <code>add</code> method should not add duplicate values.');"
},
{
"text": "Your <code>add</code> method should return <code>true</code> when a value has been successfully added.",
"testString": "assert((function(){var test = new Set(); var result = test.add('a'); return (result != undefined) && (result === true);}()), 'Your <code>add</code> method should return <code>true</code> when a value has been successfully added.');"
},
{
"text": "Your <code>add</code> method should return <code>false</code> when a duplicate value is added.",
"testString": "assert((function(){var test = new Set(); test.add('a'); var result = test.add('a'); return (result != undefined) && (result === false);}()), 'Your <code>add</code> method should return <code>false</code> when a duplicate value is added.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold our set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8253367417b2b2512c6b",
"title": "Remove from a Set",
"description": [
"In this exercises we are going to create a delete function for our set. The function should be named <code>this.remove</code>. This function should accept a value and check if it exists in the set. If it does, remove that value from the set, and return true. Otherwise, return false."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>remove</code> method.",
"testString": "assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()), 'Your <code>Set</code> class should have a <code>remove</code> method.');"
},
{
"text": "Your <code>remove</code> method should only remove items that are present in the set.",
"testString": "assert.deepEqual((function(){var test = new Set(); test.add('a');test.add('b');test.remove('c'); return test.values(); })(), ['a', 'b'], 'Your <code>remove</code> method should only remove items that are present in the set.');"
},
{
"text": "Your <code>remove</code> method should remove the given item from the set.",
"testString": "assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a'); var vals = test.values(); return (vals[0] === 'b' && vals.length === 1)}()), 'Your <code>remove</code> method should remove the given item from the set.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "8d1923c8c441eddfaeb5bdef",
"title": "Size of the Set",
"description": [
"In this exercise we are going to create a size function for our Set. This function should be named <code>this.size</code> and it should return the size of the collection."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>size</code> method.",
"testString": "assert((function(){var test = new Set(); return (typeof test.size === 'function')}()), 'Your <code>Set</code> class should have a <code>size</code> method.');"
},
{
"text": "The <code>size</code> method should return the number of elements in the collection.",
"testString": "assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a');return (test.size() === 1)}()), 'The <code>size</code> method should return the number of elements in the collection.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // this method will remove an element from a set",
" this.remove = function(element) {",
" if(this.has(element)){",
" var index = collection.indexOf(element);",
" collection.splice(index,1);",
" return true;",
" }",
" return false;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8253367417b2b2512c6c",
"title": "Perform a Union on Two Sets",
"description": [
"In this exercise we are going to perform a union on two sets of data. We will create a method on our <code>Set</code> data structure called <code>union</code>. This method should take another <code>Set</code> as an argument and return the <code>union</code> of the two sets, excluding any duplicate values.",
"For example, if <code>setA = ['a','b','c']</code> and <code>setB = ['a','b','d','e']</code>, then the union of setA and setB is: <code>setA.union(setB) = ['a', 'b', 'c', 'd', 'e']</code>."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>union</code> method.",
"testString": "assert((function(){var test = new Set(); return (typeof test.union === 'function')})(), 'Your <code>Set</code> class should have a <code>union</code> method.');"
},
{
"text": "The proper collection was returned",
"testString": "assert((function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var unionSetAB = setA.union(setB); var final = unionSetAB.values(); return (final.indexOf('a') !== -1 && final.indexOf('b') !== -1 && final.indexOf('c') !== -1 && final.indexOf('d') !== -1 && final.length === 4)})(), 'The proper collection was returned');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // this method will remove an element from a set",
" this.remove = function(element) {",
" if(this.has(element)){",
" var index = collection.indexOf(element);",
" collection.splice(index,1);",
" return true;",
" }",
" return false;",
" };",
" // this method will return the size of the set",
" this.size = function() {",
" return collection.length;",
" };",
" // change code below this line",
"",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8253367417b2b2512c6d",
"title": "Perform an Intersection on Two Sets of Data",
"description": [
"In this exercise we are going to perform an intersection on 2 sets of data. We will create a method on our <code>Set</code> data structure called <code>intersection</code>. An intersection of sets represents all values that are common to two or more sets. This method should take another <code>Set</code> as an argument and return the <code>intersection</code> of the two sets.",
"For example, if <code>setA = ['a','b','c']</code> and <code>setB = ['a','b','d','e']</code>, then the intersection of setA and setB is: <code>setA.intersection(setB) = ['a', 'b']</code>."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>intersection</code> method.",
"testString": "assert(function(){var test = new Set(); return (typeof test.intersection === 'function')}, 'Your <code>Set</code> class should have a <code>intersection</code> method.');"
},
{
"text": "The proper collection was returned",
"testString": "assert(function(){ var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var intersectionSetAB = setA.intersection(setB); return (intersectionSetAB.size() === 1 && intersectionSetAB.values()[0] === 'c')}, 'The proper collection was returned');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // this method will remove an element from a set",
" this.remove = function(element) {",
" if(this.has(element)){",
" var index = collection.indexOf(element);",
" collection.splice(index,1);",
" return true;",
" }",
" return false;",
" };",
" // this method will return the size of the collection",
" this.size = function() {",
" return collection.length;",
" };",
" // this method will return the union of two sets",
" this.union = function(otherSet) {",
" var unionSet = new Set();",
" var firstSet = this.values();",
" var secondSet = otherSet.values();",
" firstSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" secondSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" return unionSet;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8254367417b2b2512c6e",
"title": "Perform a Difference on Two Sets of Data",
"description": [
"In this exercise we are going to perform a difference on 2 sets of data. We will create a method on our <code>Set</code> data structure called <code>difference</code>. A difference of sets should compare two sets and return the items present in the first set that are absent in the second. This method should take another <code>Set</code> as an argument and return the <code>difference</code> of the two sets.",
"For example, if <code>setA = ['a','b','c']</code> and <code>setB = ['a','b','d','e']</code>, then the difference of setA and setB is: <code>setA.difference(setB) = ['c']</code>."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>difference</code> method.",
"testString": "assert(function(){var test = new Set(); return (typeof test.difference === 'function')}, 'Your <code>Set</code> class should have a <code>difference</code> method.');"
},
{
"text": "The proper collection was returned",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('c'); setB.add('d'); var differenceSetAB = setA.difference(setB); return (differenceSetAB.size() === 2) && (differenceSetAB.values() === [ 'a', 'b' ])}, 'The proper collection was returned');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};this.difference = function(set) {var d = new Set();var c = this.values();c.forEach(function(e){if(!set.has(e)) d.add(e);});};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // this method will remove an element from a set",
" this.remove = function(element) {",
" if(this.has(element)){",
" var index = collection.indexOf(element);",
" collection.splice(index,1);",
" return true;",
" }",
" return false;",
" };",
" // this method will return the size of the collection",
" this.size = function() {",
" return collection.length;",
" };",
" // this method will return the union of two sets",
" this.union = function(otherSet) {",
" var unionSet = new Set();",
" var firstSet = this.values();",
" var secondSet = otherSet.values();",
" firstSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" secondSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" return unionSet;",
" };",
" // this method will return the intersection of two sets as a new set",
" this.intersection = function(otherSet) {",
" var intersectionSet = new Set();",
" var firstSet = this.values();",
" firstSet.forEach(function(e){",
" if(otherSet.has(e)){",
" intersectionSet.add(e);",
" }",
" });",
" return intersectionSet;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8254367417b2b2512c6f",
"title": "Perform a Subset Check on Two Sets of Data",
"description": [
"In this exercise we are going to perform a subset test on 2 sets of data. We will create a method on our <code>Set</code> data structure called <code>subset</code>. This will compare the first set, against the second and if the first set is fully contained within the Second then it will return true.",
"For example, if <code>setA = ['a','b']</code> and <code>setB = ['a','b','c','d']</code>, then the subset of setA and setB is: <code>setA.subset(setB)</code> should be <code>true</code>."
],
"tests": [
{
"text": "Your <code>Set</code> class should have a <code>union</code> method.",
"testString": "assert(function(){var test = new Set(); return (typeof test.subset === 'function')}, 'Your <code>Set</code> class should have a <code>union</code> method.');"
},
{
"text": "The first Set() was contained in the second Set",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setB.add('b'); setB.add('c'); setB.add('a'); setB.add('d'); var subsetSetAB = setA.subset(setB);return (subsetSetAB === true)}, 'The first Set() was contained in the second Set');"
},
{
"text": "<code>['a', 'b'].subset(['a', 'b', 'c', 'd'])</code> should return <code>true</code>\")",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('a'); setB.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)}, \"<code>['a', 'b'].subset(['a', 'b', 'c', 'd'])</code> should return <code>true</code>\");"
},
{
"text": "<code>['a', 'b', 'c'].subset(['a', 'b'])</code> should return <code>false</code>\")",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setA.add('c'); setB.add('a'); setB.add('b'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)}, \"<code>['a', 'b', 'c'].subset(['a', 'b'])</code> should return <code>false</code>\");"
},
{
"text": "<code>[].subset([])</code> should return <code>true</code>",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); var subsetSetAB = setA.subset(setB); return (subsetSetAB === true)}, '<code>[].subset([])</code> should return <code>true</code>');"
},
{
"text": "<code>['a', 'b'].subset(['c', 'd'])</code> should return <code>false</code>\")",
"testString": "assert(function(){var setA = new Set(); var setB = new Set(); setA.add('a'); setA.add('b'); setB.add('c'); setB.add('d'); var subsetSetAB = setA.subset(setB); return (subsetSetAB === false)}, \"<code>['a', 'b'].subset(['c', 'd'])</code> should return <code>false</code>\");"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};this.difference = function(set) {var d = new Set();var c = this.values();c.forEach(function(e){if(!set.has(e)) d.add(e);});};this.subset = function(set) {var isSubset = true;var c = this.values();c.forEach(function(e){if(!set.has(e)) isSubset = false;});return isSubset;};}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function Set() {",
" // the var collection will hold the set",
" var collection = [];",
" // this method will check for the presence of an element and return true or false",
" this.has = function(element) {",
" return (collection.indexOf(element) !== -1);",
" };",
" // this method will return all the values in the set",
" this.values = function() {",
" return collection;",
" };",
" // this method will add an element to the set",
" this.add = function(element) {",
" if(!this.has(element)){",
" collection.push(element);",
" return true;",
" }",
" return false;",
" };",
" // this method will remove an element from a set",
" this.remove = function(element) {",
" if(this.has(element)){",
" var index = collection.indexOf(element);",
" collection.splice(index,1);",
" return true;",
" }",
" return false;",
" };",
" // this method will return the size of the collection",
" this.size = function() {",
" return collection.length;",
" };",
" // this method will return the union of two sets",
" this.union = function(otherSet) {",
" var unionSet = new Set();",
" var firstSet = this.values();",
" var secondSet = otherSet.values();",
" firstSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" secondSet.forEach(function(e){",
" unionSet.add(e);",
" });",
" return unionSet;",
" };",
" // this method will return the intersection of two sets as a new set",
" this.intersection = function(otherSet) {",
" var intersectionSet = new Set();",
" var firstSet = this.values();",
" firstSet.forEach(function(e){",
" if(otherSet.has(e)){",
" intersectionSet.add(e);",
" }",
" });",
" return intersectionSet;",
" };",
" // this method will return the difference of two sets as a new set",
" this.difference = function(otherSet) {",
" var differenceSet = new Set();",
" var firstSet = this.values();",
" firstSet.forEach(function(e){",
" if(!otherSet.has(e)){",
" differenceSet.add(e);",
" }",
" });",
" return differenceSet;",
" };",
" // change code below this line",
" // change code above this line",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d8254367417b2b2512c70",
"title": "Create and Add to Sets in ES6",
"description": [
"Now that you have worked through ES5, you are going to perform something similar in ES6. This will be considerably easier. ES6 contains a built-in data structure <code>Set</code> so many of the operations you wrote by hand are now included for you. Let's take a look:",
"To create a new empty set:",
"<code>var set = new Set();</code>",
"You can create a set with a value:",
"<code>var set = new Set(1);</code>",
"You can create a set with an array:",
"<code>var set = new Set([1, 2, 3]);</code>",
"Once you have created a set, you can add the values you wish using the <code>add</code> method:",
"<blockquote>var set = new Set([1, 2, 3]);<br>set.add([4, 5, 6]);</blockquote>",
"As a reminder, a set is a data structure that cannot contain duplicate values:",
"<blockquote>var set = new Set([1, 2, 3, 1, 2, 3]);<br>// set contains [1, 2, 3] only</blockquote>",
"<hr>",
"For this exercise, return a set with the following values: <code>1, 2, 3, 'Taco', 'Cat', 'Awesome'</code>"
],
"tests": [
{
"text": "Your <code>Set</code> should only contain the values <code>1, 2, 3, Taco, Cat, Awesome</code>.",
"testString": "assert(function(){var test = checkSet(); return (test.size == 6) && test.has(1) && test.has(2) && test.has(3) && test.has('Taco') && test.has('Cat') && test.has('Awesome');}, 'Your <code>Set</code> should only contain the values <code>1, 2, 3, Taco, Cat, Awesome</code>.');"
}
],
"releasedOn": "Feb 17, 2017",
"solutions": [
"function checkSet(){var set = new Set([1,2,3,'Taco','Cat','Awesome']);\nreturn set;}"
],
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function checkSet() {",
" var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);",
" // change code below this line",