forked from FreeCodeCampChina/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes6.json
More file actions
1365 lines (1365 loc) · 74 KB
/
es6.json
File metadata and controls
1365 lines (1365 loc) · 74 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": "ES6",
"order": 2,
"time": "5 hours",
"helpRoom": "Help",
"challenges": [
{
"id": "587d7b87367417b2b2512b3f",
"title": "Explore Differences Between the var and let Keywords",
"description": [
"使用 <code>var</code> 关键字来声明变量,会出现重复声明导致变量被覆盖却不会报错的问题。",
"<blockquote>var camper = 'James';<br>var camper = 'David';<br>console.log(camper);<br>// logs 'David'</blockquote>",
"看上面的代码, <code>camper</code> 最初声明为 <code>James</code>,然后又被覆盖成了 <code>David</code>。",
"在小型的应用中,你可能不会遇到这样的问题,但是当你的代码规模变得更加庞大的时候,就可能会在你不经意的时候产生变量的覆盖",
"因为这样的行为并不会报错,导致 debug 变得更加困难。<br>",
"在 ES6 中使用了新的关键字 <code>let</code> 来解决 <code>var</code> 关键字可能带来的问题。",
"如果你在上面的代码中,使用了 <code>let</code> 关键字来代替 <code>var</code>关键字,结果会是一个报错。",
"<blockquote>let camper = 'James';<br>let camper = 'David'; // throws an error</blockquote>",
"可以在可以在浏览器的控制台里看见这个错误。",
"与 <code>var</code> 不同的是, 当使用 <code>let</code> 的时候,同一名字的变量只能被声明一次。",
"请注意 <code>\"use strict\"</code>。这代表着开启了严格模式, 用于检测常见的代码错误以及\"不安全\"的行为,例如:",
"<blockquote>\"use strict\";<br>x = 3.14; // throws an error because x is not declared</blockquote>",
"<hr>",
"请更新这段代码,并且在其中只使用 <code>let</code> 关键字"
],
"tests": [
{
"text": "在代码中不应存在 <code>var</code>。",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'在代码中不应存在 <code>var</code>。');"
},
{
"text": "<code>catName</code> 变量的值应该为 <code>Oliver</code>。",
"testString": "assert(catName === \"Oliver\", '<code><code>catName</code> 变量的值应该为 <code>\"Oliver\"</code>。');"
},
{
"text": "<code>quote</code> 变量的值应该为 <code>\"Oliver says Meow!\"</code>",
"testString": "assert(quote === \"Oliver says Meow!\", '<code>quote</code> 变量的值应该为 <code>\"Oliver says Meow!\"</code>');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"var catName;",
"var quote;",
"function catTalk() {",
" \"use strict\";",
"",
" catName = \"Oliver\";",
" quote = catName + \" says Meow!\";",
"",
"}",
"catTalk();"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b87367417b2b2512b40",
"title": "Compare Scopes of the var and let Keywords",
"description": [
"当你使用 <code>var</code> 关键字来声明一个变量的时候,这个变量会被声明成全局变量,或是函数内的局部变量。",
"<code>let</code> 关键字的作用类似,但会有一些额外的特性。 当你使用 <code>let</code> 关键字在代码块,语句或者表达式里声明变量的时候,这个变量的作用域就在当前的代码块,语句或表达式之中。",
"举个例子:",
"<blockquote>var numArray = [];<br>for (var i = 0; i < 3; i++) {<br> numArray.push(i);<br>}<br>console.log(numArray);<br>// returns [0, 1, 2]<br>console.log(i);<br>// returns 3</blockquote>",
"当使用 <code>var</code> 关键字的时候, <code>i</code> 会被声明成全局变量。 当 <code>i++</code> 执行的时候, 它会改变全局变量的值。 这段代码可以看做下面这样:",
"<blockquote>var numArray = [];<br>var i;<br>for (i = 0; i < 3; i++) {<br> numArray.push(i);<br>}<br>console.log(numArray);<br>// returns [0, 1, 2]<br>console.log(i);<br>// returns 3</blockquote>",
"这个行为在你一个创建函数用来存储在 for 循环中使用的 <code>i</code> 变量的时候,会出现问题。这是因为函数存储的值会因为全局变量 <code>i</code>的变化而不断的改变。",
"<blockquote>var printNumTwo;<br>for (var i = 0; i < 3; i++) {<br> if(i === 2){<br> printNumTwo = function() {<br> return i;<br> };<br> }<br>}<br>console.log(printNumTwo());<br>// returns 3</blockquote>",
"可以看到, <code>printNumTwo()</code> 打印了 3 而不是 2。 这是因为 <code>i</code> 发生了改变,并且函数 <code>printNumTwo()</code> 返回的是全局变量 <code>i</code>的值,而不是 for 循环中创建函数时 <code>i</code> 的值。The <code>let</code> 关键字就不会有这种现象:",
"<blockquote>'use strict';<br>let printNumTwo;<br>for (let i = 0; i < 3; i++) {<br> if (i === 2) {<br> printNumTwo = function() {<br> return i;<br> };<br> }<br>}<br>console.log(printNumTwo());<br>// returns 2<br>console.log(i);<br>// returns \"i is not defined\"</blockquote>",
"<code>i</code> 在全局作用域中没有声明,所以它没有被定义, 它只会在 for 循环的语句中被声明。 因为在循环语句中的 <code>let</code>关键字创建了拥有独一无二的值 (0, 1, 或 2) 的三个不同 <code>i</code> 变量,所以 <code>printNumTwo()</code> 返回了正确的值。",
"<hr>",
"修改这段代码,使得在 if 语句中声明的 <code>i</code> 变量与在函数的第一行声明的 <code>i</code> 变量是彼此独立的。 请注意不要在你的代码的任何地方使用 <code>var</code> 关键字。",
"这个练习说明了使用 <code>var</code> 与 <code>let</code>关键字声明变量时,作用域之间的不同。当编写类似这个练习中的函数的时候,通常来说最好还是使用不同的变量名,用于避免困惑。"
],
"tests": [
{
"text": "<code>var</code> 不应该在代码中存在。",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> 不应该在代码中存在。');"
},
{
"text": "在 if 语句中声明的 <code>i</code> 变量的值是 \"block scope\"。",
"testString": "getUserInput => assert(getUserInput('index').match(/(i\\s*=\\s*).*\\s*.*\\s*.*\\1('|\")block\\s*scope\\2/g), '在 if 语句中声明的 <code>i</code> 变量应该是 \"block scope\"。');"
},
{
"text": "<code>checkScope()</code> 应当 返回 \"function scope\"",
"testString": "assert(checkScope() === \"function scope\", '<code>checkScope()</code> 应该返回 \"function scope\"');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function checkScope() {",
"\"use strict\";",
" var i = \"function scope\";",
" if (true) {",
" i = \"block scope\";",
" console.log(\"Block scope i is: \", i);",
" }",
" console.log(\"Function scope i is: \", i);",
" return i;",
"}"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b87367417b2b2512b41",
"title": "Declare a Read-Only Variable with the const Keyword",
"description": [
"<code>let</code> is not the only new way to declare variables. In ES6, you can also declare variables using the <code>const</code> keyword.",
"<code>const</code> has all the awesome features that <code>let</code> has, with the added bonus that variables declared using <code>const</code> are read-only. They are a constant value, which means that once a variable is assigned with <code>const</code>, it cannot be reassigned.",
"<blockquote>\"use strict\"<br>const FAV_PET = \"Cats\";<br>FAV_PET = \"Dogs\"; // returns error</blockquote>",
"As you can see, trying to reassign a variable declared with <code>const</code> will throw an error. You should always name variables you don't want to reassign using the <code>const</code> keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant. A common practice when naming constants is to use all uppercase letters, with words separated by an underscore.",
"<hr>",
"Change the code so that all variables are declared using <code>let</code> or <code>const</code>. Use <code>let</code> when you want the variable to change, and <code>const</code> when you want the variable to remain constant. Also, rename variables declared with <code>const</code> to conform to common practices, meaning constants should be in all caps."
],
"tests": [
{
"text": "<code>var</code> does not exist in your code.",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g),'<code>var</code> does not exist in your code.');"
},
{
"text": "<code>SENTENCE</code> should be a constant variable declared with <code>const</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/(const SENTENCE)/g), '<code>SENTENCE</code> should be a constant variable declared with <code>const</code>.');"
},
{
"text": "<code>i</code> should be declared with <code>let</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/(let i)/g), '<code>i</code> should be declared with <code>let</code>.');"
},
{
"text": "<code>console.log</code> should be changed to print the <code>SENTENCE</code> variable.",
"testString": "getUserInput => assert(getUserInput('index').match(/console\\.log\\(\\s*SENTENCE\\s*\\)\\s*;?/g), '<code>console.log</code> should be adjusted to print the variable <code>SENTENCE</code>.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function printManyTimes(str) {",
" \"use strict\";",
"",
" // change code below this line",
"",
" var sentence = str + \" is cool!\";",
" for(var i = 0; i < str.length; i+=2) {",
" console.log(sentence);",
" }",
"",
" // change code above this line",
"",
"}",
"printManyTimes(\"freeCodeCamp\");"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b87367417b2b2512b42",
"title": "Mutate an Array Declared with const",
"description": [
"The <code>const</code> declaration has many use cases in modern JavaScript.",
"Some developers prefer to assign all their variables using <code>const</code> by default, unless they know they will need to reassign the value. Only in that case, they use <code>let</code>.",
"However, it is important to understand that objects (including arrays and functions) assigned to a variable using <code>const</code> are still mutable. Using the <code>const</code> declaration only prevents reassignment of the variable identifier.",
"<blockquote>\"use strict\";<br>const s = [5, 6, 7];<br>s = [1, 2, 3]; // throws error, trying to assign a const<br>s[2] = 45; // works just as it would with an array declared with var or let<br>console.log(s); // returns [5, 6, 45]</blockquote>",
"As you can see, you can mutate the object <code>[5, 6, 7]</code> itself and the variable <code>s</code> will still point to the altered array <code>[5, 6, 45]</code>. Like all arrays, the array elements in <code>s</code> are mutable, but because <code>const</code> was used, you cannot use the variable identifier <code>s</code> to point to a different array using the assignment operator.",
"<hr>",
"An array is declared as <code>const s = [5, 7, 2]</code>. Change the array to <code>[2, 5, 7]</code> using various element assignment."
],
"tests": [
{
"text": "Do not replace <code>const</code> keyword.",
"testString": "getUserInput => assert(getUserInput('index').match(/const/g), 'Do not replace <code>const</code> keyword.');"
},
{
"text": "<code>s</code> should be a constant variable (by using <code>const</code>).",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+s/g), '<code>s</code> should be a constant variable (by using <code>const</code>).');"
},
{
"text": "Do not change the original array declaration.",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+s\\s*=\\s*\\[\\s*5\\s*,\\s*7\\s*,\\s*2\\s*\\]\\s*;?/g), 'Do not change the original array declaration.');"
},
{
"text": "<code>s</code> should be equal to <code>[2, 5, 7]</code>.",
"testString": "assert.deepEqual(s, [2, 5, 7], '<code>s</code> should be equal to <code>[2, 5, 7]</code>.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const s = [5, 7, 2];",
"function editInPlace() {",
" \"use strict\";",
" // change code below this line",
"",
" // s = [2, 5, 7]; <- this is invalid",
"",
" // change code above this line",
"}",
"editInPlace();"
],
"head": [],
"tail": []
}
}
},
{
"id": "598f48a36c8c40764b4e52b3",
"title": "Prevent Object Mutation",
"description": [
"As seen in the previous challenge, <code>const</code> declaration alone doesn't really protect your data from mutation. To ensure your data doesn't change, JavaScript provides a function <code>Object.freeze</code> to prevent data mutation.",
"Once the object is frozen, you can no longer add, update, or delete properties from it. Any attempt at changing the object will be rejected without an error.",
"<blockquote>let obj = {<br> name:\"FreeCodeCamp\",<br> review:\"Awesome\"<br>};<br>Object.freeze(obj);<br>obj.review = \"bad\"; //will be ignored. Mutation not allowed<br>obj.newProp = \"Test\"; // will be ignored. Mutation not allowed<br>console.log(obj); <br>// { name: \"FreeCodeCamp\", review:\"Awesome\"}</blockquote>",
"<hr>",
"In this challenge you are going to use <code>Object.freeze</code> to prevent mathematical constants from changing. You need to freeze the <code>MATH_CONSTANTS</code> object so that no one is able alter the value of <code>PI</code>, add, or delete properties ."
],
"tests": [
{
"text": "Do not replace <code>const</code> keyword.",
"testString": "getUserInput => assert(getUserInput('index').match(/const/g), 'Do not replace <code>const</code> keyword.');"
},
{
"text": "<code>MATH_CONSTANTS</code> should be a constant variable (by using <code>const</code>).",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+MATH_CONSTANTS/g), '<code>MATH_CONSTANTS</code> should be a constant variable (by using <code>const</code>).');"
},
{
"text": "Do not change original <code>MATH_CONSTANTS</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+MATH_CONSTANTS\\s+=\\s+{\\s+PI:\\s+3.14\\s+};/g), 'Do not change original <code>MATH_CONSTANTS</code>.');"
},
{
"text": "<code>PI</code> equals <code>3.14</code>.",
"testString": "assert(PI === 3.14, '<code>PI</code> equals <code>3.14</code>.');"
}
],
"releasedOn": "Aug 12, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function freezeObj() {",
" \"use strict\";",
" const MATH_CONSTANTS = {",
" PI: 3.14",
" };",
" // change code below this line",
"",
"",
" // change code above this line",
" try {",
" MATH_CONSTANTS.PI = 99;",
" } catch( ex ) {",
" console.log(ex);",
" }",
" return MATH_CONSTANTS.PI;",
"}",
"const PI = freezeObj();"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b87367417b2b2512b43",
"title": "Use Arrow Functions to Write Concise Anonymous Functions",
"description": [
"In JavaScript, we often don't need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don't need to name these functions because we do not reuse them anywhere else.",
"To achieve this, we often use the following syntax:",
"<blockquote>const myFunc = function() {<br> const myVar = \"value\";<br> return myVar;<br>}</blockquote>",
"ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use <strong>arrow function syntax</strong>:",
"<blockquote>const myFunc = () => {<br> const myVar = \"value\";<br> return myVar;<br>}</blockquote>",
"When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword <code>return</code> as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:",
"<blockquote>const myFunc= () => \"value\"</blockquote>",
"This code will still return <code>value</code> by default.",
"<hr>",
"Rewrite the function assigned to the variable <code>magic</code> which returns a new <code>Date()</code> to use arrow function syntax. Also make sure nothing is defined using the keyword <code>var</code>."
],
"tests": [
{
"text": "User did replace <code>var</code> keyword.",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g), 'User did replace <code>var</code> keyword.');"
},
{
"text": "<code>magic</code> should be a constant variable (by using <code>const</code>).",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+magic/g), '<code>magic</code> should be a constant variable (by using <code>const</code>).');"
},
{
"text": "<code>magic</code> is a <code>function</code>.",
"testString": "assert(typeof magic === 'function', '<code>magic</code> is a <code>function</code>.');"
},
{
"text": "<code>magic()</code> returns correct date.",
"testString": "assert(magic().getDate() == new Date().getDate(), '<code>magic()</code> returns correct date.');"
},
{
"text": "<code>function</code> keyword was not used.",
"testString": "getUserInput => assert(!getUserInput('index').match(/function/g), '<code>function</code> keyword was not used.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"var magic = function() {",
" \"use strict\";",
" return new Date();",
"};"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b88367417b2b2512b44",
"title": "Write Arrow Functions with Parameters",
"description": [
"Just like a normal function, you can pass arguments into arrow functions.",
"<blockquote>// doubles input value and returns it<br>const doubler = (item) => item * 2;</blockquote>",
"You can pass more than one argument into arrow functions as well.",
"<hr>",
"Rewrite the <code>myConcat</code> function which appends contents of <code>arr2</code> to <code>arr1</code> so that the function uses arrow function syntax."
],
"tests": [
{
"text": "User did replace <code>var</code> keyword.",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g), 'User did replace <code>var</code> keyword.');"
},
{
"text": "<code>myConcat</code> should be a constant variable (by using <code>const</code>).",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+myConcat/g), '<code>myConcat</code> should be a constant variable (by using <code>const</code>).');"
},
{
"text": "<code>myConcat</code> should be a function",
"testString": "assert(typeof myConcat === 'function', '<code>myConcat</code> should be a function');"
},
{
"text": "<code>myConcat()</code> returns the correct <code>array</code>",
"testString": "assert(() => { const a = myConcat([1], [2]); return a[0] == 1 && a[1] == 2; }, '<code>myConcat()</code> returns the correct <code>array</code>');"
},
{
"text": "<code>function</code> keyword was not used.",
"testString": "getUserInput => assert(!getUserInput('index').match(/function/g), '<code>function</code> keyword was not used.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"var myConcat = function(arr1, arr2) {",
" \"use strict\";",
" return arr1.concat(arr2);",
"};",
"// test your code",
"console.log(myConcat([1, 2], [3, 4, 5]));"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b88367417b2b2512b45",
"title": "Write Higher Order Arrow Functions",
"description": [
"It's time we see how powerful arrow functions are when processing data.",
"Arrow functions work really well with higher order functions, such as <code>map()</code>, <code>filter()</code>, and <code>reduce()</code>, that take other functions as arguments for processing collections of data.",
"Read the following code:",
"<blockquote>FBPosts.filter(function(post) {<br> return post.thumbnail !== null && post.shares > 100 && post.likes > 500;<br>})</blockquote>",
"We have written this with <code>filter()</code> to at least make it somewhat readable. Now compare it to the following code which uses arrow function syntax instead:",
"<blockquote>FBPosts.filter((post) => post.thumbnail !== null && post.shares > 100 && post.likes > 500)</blockquote>",
"This code is more succinct and accomplishes the same task with fewer lines of code.",
"<hr>",
"Use arrow function syntax to compute the square of only the positive integers (fractions are not integers) in the array <code>realNumberArray</code> and store the new array in the variable <code>squaredIntegers</code>."
],
"tests": [
{
"text": "User did replace <code>var</code> keyword.",
"testString": "getUserInput => assert(!getUserInput('index').match(/var/g), 'User did replace <code>var</code> keyword.');"
},
{
"text": "<code>squaredIntegers</code> should be a constant variable (by using <code>const</code>).",
"testString": "getUserInput => assert(getUserInput('index').match(/const\\s+squaredIntegers/g), '<code>squaredIntegers</code> should be a constant variable (by using <code>const</code>).');"
},
{
"text": "<code>squaredIntegers</code> should be an <code>array</code>",
"testString": "assert(Array.isArray(squaredIntegers), '<code>squaredIntegers</code> should be an <code>array</code>');"
},
{
"text": "<code>squaredIntegers</code> should be <code>[16, 1764, 36]</code>",
"testString": "assert(squaredIntegers[0] === 16 && squaredIntegers[1] === 1764 && squaredIntegers[2] === 36, '<code>squaredIntegers</code> should be <code>[16, 1764, 36]</code>');"
},
{
"text": "<code>function</code> keyword was not used.",
"testString": "getUserInput => assert(!getUserInput('index').match(/function/g), '<code>function</code> keyword was not used.');"
},
{
"text": "loop should not be used",
"testString": "getUserInput => assert(!getUserInput('index').match(/(for)|(while)/g), 'loop should not be used');"
},
{
"text": "<code>map</code>, <code>filter</code>, or <code>reduce</code> should be used",
"testString": "getUserInput => assert(getUserInput('index').match(/map|filter|reduce/g), '<code>map</code>, <code>filter</code>, or <code>reduce</code> should be used');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];",
"const squareList = (arr) => {",
" \"use strict\";",
" // change code below this line",
" const squaredIntegers = arr;",
" // change code above this line",
" return squaredIntegers;",
"};",
"// test your code",
"const squaredIntegers = squareList(realNumberArray);",
"console.log(squaredIntegers);"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b88367417b2b2512b46",
"title": "Set Default Parameters for Your Functions",
"description": [
"In order to help us create more flexible functions, ES6 introduces <dfn>default parameters</dfn> for functions.",
"Check out this code:",
"<blockquote>function greeting(name = \"Anonymous\") {<br> return \"Hello \" + name;<br>}<br>console.log(greeting(\"John\")); // Hello John<br>console.log(greeting()); // Hello Anonymous</blockquote>",
"The default parameter kicks in when the argument is not specified (it is undefined). As you can see in the example above, the parameter <code>name</code> will receive its default value <code>\"Anonymous\"</code> when you do not provide a value for the parameter. You can add default values for as many parameters as you want.",
"<hr>",
"Modify the function <code>increment</code> by adding default parameters so that it will add 1 to <code>number</code> if <code>value</code> is not specified."
],
"tests": [
{
"text": "The result of <code>increment(5, 2)</code> should be <code>7</code>.",
"testString": "assert(increment(5, 2) === 7, 'The result of <code>increment(5, 2)</code> should be <code>7</code>.');"
},
{
"text": "The result of <code>increment(5)</code> should be <code>6</code>.",
"testString": "assert(increment(5) === 6, 'The result of <code>increment(5)</code> should be <code>6</code>.');"
},
{
"text": "default parameter <code>1</code> was used for <code>value</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/value\\s*=\\s*1/g), 'default parameter <code>1</code> was used for <code>value</code>.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const increment = (function() {",
" \"use strict\";",
" return function increment(number, value) {",
" return number + value;",
" };",
"})();",
"console.log(increment(5, 2)); // returns 7",
"console.log(increment(5)); // returns NaN"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b88367417b2b2512b47",
"title": "Use the Rest Operator with Function Parameters",
"description": [
"In order to help us create more flexible functions, ES6 introduces the <dfn>rest operator</dfn> for function parameters. With the rest operator, you can create functions that take a variable number of arguments. These arguments are stored in an array that can be accessed later from inside the function.",
"Check out this code:",
"<blockquote>function howMany(...args) {<br> return \"You have passed \" + args.length + \" arguments.\";<br>}<br>console.log(howMany(0, 1, 2)); // You have passed 3 arguments<br>console.log(howMany(\"string\", null, [1, 2, 3], { })); // You have passed 4 arguments.</blockquote>",
"The rest operator eliminates the need to check the <code>args</code> array and allows us to apply <code>map()</code>, <code>filter()</code> and <code>reduce()</code> on the parameters array.",
"<hr>",
"Modify the function <code>sum</code> so that it uses the rest operator and it works in the same way with any number of parameters."
],
"tests": [
{
"text": "The result of <code>sum(0,1,2)</code> should be 3",
"testString": "assert(sum(0,1,2) === 3, 'The result of <code>sum(0,1,2)</code> should be 3');"
},
{
"text": "The result of <code>sum(1,2,3,4)</code> should be 10",
"testString": "assert(sum(1,2,3,4) === 10, 'The result of <code>sum(1,2,3,4)</code> should be 10');"
},
{
"text": "The result of <code>sum(5)</code> should be 5",
"testString": "assert(sum(5) === 5, 'The result of <code>sum(5)</code> should be 5');"
},
{
"text": "The result of <code>sum()</code> should be 0",
"testString": "assert(sum() === 0, 'The result of <code>sum()</code> should be 0');"
},
{
"text": "The <code>sum</code> function uses the <code>...</code> spread operator on the <code>args</code> parameter.",
"testString": "getUserInput => assert(getUserInput('index').match(/function\\s+sum\\s*\\(\\s*...args\\s*\\)\\s*{/g), 'The <code>sum</code> function uses the <code>...</code> spread operator on the <code>args</code> parameter.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const sum = (function() {",
" \"use strict\";",
" return function sum(x, y, z) {",
" const args = [ x, y, z ];",
" return args.reduce((a, b) => a + b, 0);",
" };",
"})();",
"console.log(sum(1, 2, 3)); // 6"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b89367417b2b2512b48",
"title": "Use the Spread Operator to Evaluate Arrays In-Place",
"description": [
"ES6 introduces the <dfn>spread operator</dfn>, which allows us to expand arrays and other expressions in places where multiple parameters or elements are expected.",
"The ES5 code below uses <code>apply()</code> to compute the maximum value in an array:",
"<blockquote>var arr = [6, 89, 3, 45];<br>var maximus = Math.max.apply(null, arr); // returns 89</blockquote>",
"We had to use <code>Math.max.apply(null, arr)</code> because <code>Math.max(arr)</code> returns <code>NaN</code>. <code>Math.max()</code> expects comma-separated arguments, but not an array.",
"The spread operator makes this syntax much better to read and maintain.",
"<blockquote>const arr = [6, 89, 3, 45];<br>const maximus = Math.max(...arr); // returns 89</blockquote>",
"<code>...arr</code> returns an unpacked array. In other words, it <em>spreads</em> the array.",
"However, the spread operator only works in-place, like in an argument to a function or in an array literal. The following code will not work:",
"<blockquote>const spreaded = ...arr; // will throw a syntax error</blockquote>",
"<hr>",
"Copy all contents of <code>arr1</code> into another array <code>arr2</code> using the spread operator."
],
"tests": [
{
"text": "<code>arr2</code> is correct copy of <code>arr1</code>.",
"testString": "assert(arr2.every((v, i) => v === arr1[i]), '<code>arr2</code> is correct copy of <code>arr1</code>.');"
},
{
"text": "<code>...</code> spread operator was used to duplicate <code>arr1</code>.",
"testString": "getUserInput => assert(getUserInput('index').match(/\\[\\s*...arr1\\s*\\]/g),'<code>...</code> spread operator was used to duplicate <code>arr1</code>.');"
},
{
"text": "<code>arr2</code> remains unchanged when <code>arr1</code> is changed.",
"testString": "assert((arr1, arr2) => {arr1.push('JUN'); return arr2.length < arr1.length},'<code>arr2</code> remains unchanged when <code>arr1</code> is changed.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];",
"let arr2;",
"(function() {",
" \"use strict\";",
" arr2 = []; // change this line",
"})();",
"console.log(arr2);"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b89367417b2b2512b49",
"title": "Use Destructuring Assignment to Assign Variables from Objects",
"description": [
"We saw earlier how spread operator can effectively spread, or unpack, the contents of the array.",
"We can do something similar with objects as well. <dfn>Destructuring assignment</dfn> is special syntax for neatly assigning values taken directly from an object to variables.",
"Consider the following ES5 code:",
"<blockquote>var voxel = {x: 3.6, y: 7.4, z: 6.54 };<br>var x = voxel.x; // x = 3.6<br>var y = voxel.y; // y = 7.4<br>var z = voxel.z; // z = 6.54</blockquote>",
"Here's the same assignment statement with ES6 destructuring syntax:",
"<blockquote>const { x, y, z } = voxel; // x = 3.6, y = 7.4, z = 6.54</blockquote>",
"If instead you want to store the values of <code>voxel.x</code> into <code>a</code>, <code>voxel.y</code> into <code>b</code>, and <code>voxel.z</code> into <code>c</code>, you have that freedom as well.",
"<blockquote>const { x : a, y : b, z : c } = voxel // a = 3.6, b = 7.4, c = 6.54</blockquote>",
"You may read it as \"get the field <code>x</code> and copy the value into <code>a</code>,\" and so on.",
"<hr>",
"Use destructuring to obtain the length of the input string <code>str</code>, and assign the length to <code>len</code> in line."
],
"tests": [
{
"text": "the function <code>getLength()</code> returns a number.",
"testString": "assert(typeof getLength('') === 'number', 'the function <code>getLength()</code> returns a number.');"
},
{
"text": "<code>getLength(\"FreeCodeCamp\")</code> should be <code>12</code>",
"testString": "assert(getLength(\"FreeCodeCamp\") === 12, '<code>getLength(\"FreeCodeCamp\")</code> should be <code>12</code>');"
},
{
"text": "destructuring with reassignment was used",
"testString": "getUserInput => assert(getUserInput('index').match(/\\{\\s*length\\s*:\\s*len\\s*}\\s*=\\s*str/g),'destructuring with reassignment was used');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"function getLength(str) {",
" \"use strict\";",
"",
" // change code below this line",
" const length = 0; // change this",
" // change code above this line",
"",
" return len; // you must assign length to len in line",
"",
"}",
"",
"console.log(getLength('FreeCodeCamp'));"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b89367417b2b2512b4a",
"title": "Use Destructuring Assignment to Assign Variables from Nested Objects",
"description": [
"We can similarly destructure <em>nested</em> objects into variables.",
"Consider the following code:",
"<blockquote>const a = {<br> start: { x: 5, y: 6},<br> end: { x: 6, y: -9 }<br>};<br>const { start : { x: startX, y: startY }} = a;<br>console.log(startX, startY); // 5, 6</blockquote>",
"In the example above, the variable <code>start</code> is assigned the value of <code>a.start</code>, which is also an object.",
"<hr>",
"Use destructuring assignment to obtain <code>max</code> of <code>forecast.tomorrow</code> and assign it to <code>maxOfTomorrow</code>."
],
"tests": [
{
"text": "<code>maxOfTomorrow</code> equals <code>84.6</code>",
"testString": "assert(getMaxOfTmrw(LOCAL_FORECAST) === 84.6, '<code>maxOfTomorrow</code> equals <code>84.6</code>');"
},
{
"text": "nested destructuring was used",
"testString": "getUserInput => assert(getUserInput('index').match(/\\{\\s*tomorrow\\s*:\\s*\\{\\s*max\\s*:\\s*maxOfTomorrow\\s*\\}\\s*\\}\\s*=\\s*forecast/g),'nested destructuring was used');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const LOCAL_FORECAST = {",
" today: { min: 72, max: 83 },",
" tomorrow: { min: 73.3, max: 84.6 }",
"};",
"",
"function getMaxOfTmrw(forecast) {",
" \"use strict\";",
" // change code below this line",
" const maxOfTomorrow = undefined; // change this line",
" // change code above this line",
" return maxOfTomorrow;",
"}",
"",
"console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b89367417b2b2512b4b",
"title": "Use Destructuring Assignment to Assign Variables from Arrays",
"description": [
"ES6 makes destructuring arrays as easy as destructuring objects.",
"One key difference between the spread operator and array destructuring is that the spread operator unpacks all contents of an array into a comma-separated list. Consequently, you cannot pick or choose which elements you want to assign to variables.",
"Destructuring an array lets us do exactly that:",
"<blockquote>const [a, b] = [1, 2, 3, 4, 5, 6];<br>console.log(a, b); // 1, 2</blockquote>",
"The variable <code>a</code> is assigned the first value of the array, and <code>b</code> is assigned the second value of the array.",
"We can also access the value at any index in an array with destructuring by using commas to reach the desired index:",
"<blockquote>const [a, b,,, c] = [1, 2, 3, 4, 5, 6];<br>console.log(a, b, c); // 1, 2, 5 </blockquote>",
"<hr>",
"Use destructuring assignment to swap the values of <code>a</code> and <code>b</code> so that <code>a</code> receives the value stored in <code>b</code>, and <code>b</code> receives the value stored in <code>a</code>."
],
"tests": [
{
"text": "Value of <code>a</code> should be 6, after swapping.",
"testString": "assert(a === 6, 'Value of <code>a</code> should be 6, after swapping.');"
},
{
"text": "Value of <code>b</code> should be 8, after swapping.",
"testString": "assert(b === 8, 'Value of <code>b</code> should be 8, after swapping.');"
},
{
"text": "Use array destructuring to swap a and b.",
"testString": "// assert(/\\[\\s*(\\w)\\s*,\\s*(\\w)\\s*\\]\\s*=\\s*\\[\\s*\\2\\s*,\\s*\\1\\s*\\]/g.test(code), 'Use array destructuring to swap a and b.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"let a = 8, b = 6;",
"(() => {",
" \"use strict\";",
" // change code below this line",
" ",
" // change code above this line",
"})();",
"console.log(a); // should be 6",
"console.log(b); // should be 8"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b8a367417b2b2512b4c",
"title": "Use Destructuring Assignment with the Rest Operator to Reassign Array Elements",
"description": [
"In some situations involving array destructuring, we might want to collect the rest of the elements into a separate array.",
"The result is similar to <code>Array.prototype.slice()</code>, as shown below:",
"<blockquote>const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];<br>console.log(a, b); // 1, 2<br>console.log(arr); // [3, 4, 5, 7]</blockquote>",
"Variables <code>a</code> and <code>b</code> take the first and second values from the array. After that, because of rest operator's presence, <code>arr</code> gets rest of the values in the form of an array.",
"The rest element only works correctly as the last variable in the list. As in, you cannot use the rest operator to catch a subarray that leaves out last element of the original array.",
"<hr>",
"Use destructuring assignment with the rest operator to perform an effective <code>Array.prototype.slice()</code> so that <code>arr</code> is a sub-array of the original array <code>source</code> with the first two elements omitted."
],
"tests": [
{
"text": "<code>arr</code> should be <code>[3,4,5,6,7,8,9,10]</code>",
"testString": "assert(arr.every((v, i) => v === i + 3),'<code>arr</code> should be <code>[3,4,5,6,7,8,9,10]</code>');"
},
{
"text": "destructuring was used.",
"testString": "getUserInput => assert(getUserInput('index').match(/\\[\\s*\\w*\\s*,\\s*\\w*\\s*,\\s*...arr\\s*\\]/g),'destructuring was used.');"
},
{
"text": "<code>Array.slice()</code> was not used.",
"testString": "getUserInput => assert(!getUserInput('index').match(/Array.slice/g), '<code>Array.slice()</code> was not used.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const source = [1,2,3,4,5,6,7,8,9,10];",
"function removeFirstTwo(list) {",
" \"use strict\";",
" // change code below this line",
" arr = list; // change this",
" // change code above this line",
" return arr;",
"}",
"const arr = removeFirstTwo(source);",
"console.log(arr); // should be [3,4,5,6,7,8,9,10]",
"console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b8a367417b2b2512b4d",
"title": "Use Destructuring Assignment to Pass an Object as a Function's Parameters",
"description": [
"In some cases, you can destructure the object in a function argument itself.",
"Consider the code below:",
"<blockquote>const profileUpdate = (profileData) => {<br> const { name, age, nationality, location } = profileData;<br> // do something with these variables<br>}</blockquote>",
"This effectively destructures the object sent into the function. This can also be done in-place:",
"<blockquote>const profileUpdate = ({ name, age, nationality, location }) => {<br> /* do something with these fields */<br>}</blockquote>",
"This removes some extra lines and makes our code look neat.",
"This has the added benefit of not having to manipulate an entire object in a function; only the fields that are needed are copied inside the function.",
"<hr>",
"Use destructuring assignment within the argument to the function <code>half</code> to send only <code>max</code> and <code>min</code> inside the function."
],
"tests": [
{
"text": "<code>stats</code> should be an <code>object</code>.",
"testString": "assert(typeof stats === 'object', '<code>stats</code> should be an <code>object</code>.');"
},
{
"text": "<code>half(stats)</code> should be <code>28.015</code>",
"testString": "assert(half(stats) === 28.015, '<code>half(stats)</code> should be <code>28.015</code>');"
},
{
"text": "Destructuring was used.",
"testString": "getUserInput => assert(getUserInput('index').match(/\\(\\s*\\{\\s*\\w+\\s*,\\s*\\w+\\s*\\}\\s*\\)/g), 'Destructuring was used.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const stats = {",
" max: 56.78,",
" standard_deviation: 4.34,",
" median: 34.54,",
" mode: 23.87,",
" min: -0.75,",
" average: 35.85",
"};",
"const half = (function() {",
" \"use strict\"; // do not change this line",
"",
" // change code below this line",
" return function half(stats) {",
" // use function argument destructuring",
" return (stats.max + stats.min) / 2.0;",
" };",
" // change code above this line",
"",
"})();",
"console.log(stats); // should be object",
"console.log(half(stats)); // should be 28.015"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b8a367417b2b2512b4e",
"title": "Create Strings using Template Literals",
"description": [
"A new feature of ES6 is the <dfn>template literal</dfn>. This is a special type of string that allows you to use string interpolation features to create strings.",
"Consider the code below:",
"<blockquote>const person = {<br> name: \"Zodiac Hasbro\",<br> age: 56<br>};<br><br>// string interpolation<br>const greeting = `Hello, my name is ${person.name}!<br>I am ${person.age} years old.`;<br><br>console.log(greeting); // prints<br>// Hello, my name is Zodiac Hasbro!<br>// I am 56 years old.<br></blockquote>",
"A lot of things happened there.",
"Firstly, the <code>${variable}</code> syntax used above is a place holder. Basically, you won't have to use concatenation with the <code>+</code> operator anymore. To add variables to strings, you just drop the variable in a template string and wrap it with <code>${</code> and <code>}</code>.",
"Secondly, the example uses backticks (<code>`</code>), not quotes (<code>'</code> or <code>\"</code>), to wrap the string. Notice that the string is multi-line.",
"This new way of creating strings gives you more flexibility to create robust strings.",
"<hr>",
"Use template literal syntax with backticks to display each entry of the <code>result</code> object's <code>failure</code> array. Each entry should be wrapped inside an <code>li</code> element with the class attribute <code>text-warning</code>, and listed within the <code>resultDisplayArray</code>."
],
"tests": [
{
"text": "<code>resultDisplayArray</code> is a list containing <code>result failure</code> messages.",
"testString": "assert(typeof makeList(result.failure) === 'object' && resultDisplayArray.length === 3, '<code>resultDisplayArray</code> is a list containing <code>result failure</code> messages.');"
},
{
"text": "<code>resultDisplayArray</code> is the desired output.",
"testString": "assert(makeList(result.failure).every((v, i) => v === `<li class=\"text-warning\">${result.failure[i]}</li>`), '<code>resultDisplayArray</code> is the desired output.');"
},
{
"text": "Template strings were used",
"testString": "getUserInput => assert(getUserInput('index').match(/`<li \\s*class\\s*=\\s*(\"\\s*text-warning\\s*\"|'\\s*text-warning\\s*')\\s*>\\s*\\$\\s*\\{(\\s*\\w+\\s*|\\s*\\w+\\s*\\[\\s*[\\w]+\\s*\\]\\s*)\\}\\s*<\\s*\\/li\\s*>`/g), 'Template strings were used');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const result = {",
" success: [\"max-length\", \"no-amd\", \"prefer-arrow-functions\"],",
" failure: [\"no-var\", \"var-on-top\", \"linebreak\"],",
" skipped: [\"id-blacklist\", \"no-dup-keys\"]",
"};",
"function makeList(arr) {",
" \"use strict\";",
"",
" // change code below this line",
" const resultDisplayArray = null;",
" // change code above this line",
"",
" return resultDisplayArray;",
"}",
"/**",
" * makeList(result.failure) should return:",
" * [ <li class=\"text-warning\">no-var</li>,",
" * <li class=\"text-warning\">var-on-top</li>, ",
" * <li class=\"text-warning\">linebreak</li> ]",
" **/",
"const resultDisplayArray = makeList(result.failure);"
],
"head": [],
"tail": []
}
}
},
{
"id": "587d7b8a367417b2b2512b4f",
"title": "Write Concise Object Literal Declarations Using Simple Fields",
"description": [
"ES6 adds some nice support for easily defining object literals.",
"Consider the following code:",
"<blockquote>const getMousePosition = (x, y) => ({<br> x: x,<br> y: y<br>});</blockquote>",
"<code>getMousePosition</code> is a simple function that returns an object containing two fields.",
"ES6 provides the syntactic sugar to eliminate the redundancy of having to write <code>x: x</code>. You can simply write <code>x</code> once, and it will be converted to<code>x: x</code> (or something equivalent) under the hood.",
"Here is the same function from above rewritten to use this new syntax:",
"<blockquote>const getMousePosition = (x, y) => ({ x, y });</blockquote>",
"<hr>",
"Use simple fields with object literals to create and return a <code>Person</code> object."
],
"tests": [
{
"text": "the output is <code>{name: \"Zodiac Hasbro\", age: 56, gender: \"male\"}</code>.",
"testString": "assert(() => {const res={name:\"Zodiac Hasbro\",age:56,gender:\"male\"}; const person=createPerson(\"Zodiac Hasbro\", 56, \"male\"); return Object.keys(person).every(k => person[k] === res[k]);}, 'the output is <code>{name: \"Zodiac Hasbro\", age: 56, gender: \"male\"}</code>.');"
},
{
"text": "No <code>:</code> were used.",
"testString": "getUserInput => assert(!getUserInput('index').match(/:/g), 'No <code>:</code> were used.');"
}
],
"releasedOn": "Feb 17, 2017",
"challengeType": 1,
"files": {
"indexjs": {
"key": "indexjs",
"ext": "js",
"name": "index",
"contents": [
"const createPerson = (name, age, gender) => {",
" \"use strict\";",
" // change code below this line",
" return {",
" name: name,",
" age: age,",
" gender: gender",
" };",
" // change code above this line",
"};",
"console.log(createPerson(\"Zodiac Hasbro\", 56, \"male\")); // returns a proper object"
],
"head": [],
"tail": []
}
}