Js-A.m.a601 (Es6 01)
Js-A.m.a601 (Es6 01)
Js-A.m.a601 (Es6 01)
T ra in in g As s ig n men t
Version 1.1
Hanoi, mm/yyyy
Training Assignments Front-end Advanced Issue/Revision: x/y
RECORD OF CHANGES
Contents
Day 9-10. Unit 5: ES6 New Syntax ....................................................................................................................4
Objectives: ..................................................................................................................................................4
Problem 01 .................................................................................................................................................4
Problem 02 .................................................................................................................................................5
Problem 03 .................................................................................................................................................5
Problem 04 .................................................................................................................................................5
Problem 05 .................................................................................................................................................6
Problem 01
Given the following code, your task is to change function declaration/function expression of ES5 to Arrow
Function syntax, however you have to make sure the output is still `true`
1. (function IIFE() {
2. function foo(x) {
3. var y = x * 2;
4.
5. return function bar(z) {
6. if (z.length > 3) {
7. return z.map(function baz(v) {
8. if (v > 3) return v + y;
9. else return baz(v * 4);
10. });
11. } else {
12. var obj = [];
13.
14. setTimeout(
15. function bam() {
16. obj.length = 1;
17. obj[0] = this.w;
18. }.bind(this),
19. 100
20. );
21.
22. return obj;
23. }
24. };
25. }
26.
27. var p = foo(2);
28. var list1 = [1, 3, 4];
29. var list2 = list1.concat(6);
30.
31. list1 = p.call({ w: 42 }, list1);
32. list2 = p(list2);
33.
34. setTimeout(function() {
35. console.log(
36. list1[0] ===
37. list2.reduce(function(s, v) {
38. return s + v;
39. }, 0)
40. );
41. }, 200);
42. })();
Problem 02
Fix the following code, so the output is `true`.
1. var x = 2,
2. fns = [];
3.
4. (function() {
5. var x = 5;
6.
7. for (var i = 0; i < x; i++) {
8. // ..
9. }
10. })();
11.
12. // DO NOT MODIFY BELOW CODE
13. console.log(x * 2 === fns[x * 2]());
14. // true
Problem 03
Use rest/spread operator so the code below display `true`
1. function foo() {}
2.
3. function bar() {
4. var a1 = [2, 4];
5. var a2 = [6, 8, 10, 12];
6.
7. return foo();
8. }
9.
10. // DO NOT MODIFY BELOW CODE
11. console.log(bar().join('') === '281012');
12. // true
Problem 04
Given the following code, you must use ES6 Destructuring feature to construct a data for function check so
the output is `true`.
21. data.bam.qam
22. );
23. }
24.
25. var defaults = {
26. foo: 0,
27. bar: 4,
28. bam: {
29. qux: 0,
30. qam: 14
31. }
32. };
33.
34. // YOUR CODE HERE
35. function response(...) {
36. check(...); // true
37. }
38.
39. // DO NOT MODIFY
40. ajax('http://fun.tld', response);
Problem 05
Given the following code, you must complete the upper function and use it as a tag function for the template
string `Hello ____ (@____), welcome to the ____!!!` so the output is `true`