|
1 | 1 | # Homework Week 8 |
2 | | - |
3 | | -http://conceptf1.blogspot.nl/2013/11/javascript-closures.html https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/ http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html https://medium.freecodecamp.com/5-javascript-bad-parts-that-are-fixed-in-es6-c7c45d44fd81 |
4 | | - |
5 | | -https://www.reddit.com/r/learnjavascript/comments/1v6n8p/closure_explain_likei_am_in_high_school/?st=ixsp0mbe&sh=5526d150 A VERY popular StackOverflow article: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work |
6 | | - |
7 | | -1. Let's continue to learn a little more about scope and Closures. |
8 | | - |
9 | | - |
10 | | -2. What will be the output of the following code - and more importantly - WHY? |
11 | | - |
12 | | -```js |
13 | | -for (var i = 0; i < 3; i++) { |
14 | | - setTimeout(function() { alert(i); }, 1000 + i); |
15 | | -} |
16 | | -``` |
17 | | - |
18 | | - |
19 | | -3. Write a function that would allow you to do this: |
20 | | -```js |
21 | | -var addSix = createBase(6); |
22 | | -addSix(10); // returns 16 |
23 | | -addSix(21); // returns 27 |
24 | | -``` |
25 | | - |
26 | | -4. You will need to create an HTML document out of the below snippet to run the below code. A hint - the code is syntactically correct but doesn't do what you would expect. Can you see why and fix it? |
27 | | - |
28 | | -Don't cheat - but if you get stuck ... http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example |
29 | | - |
30 | | -```html |
31 | | -<button id="btn-0">Button 1!</button> |
32 | | -<button id="btn-1">Button 2!</button> |
33 | | -<button id="btn-2">Button 3!</button> |
34 | | - |
35 | | -<script type="text/javascript"> |
36 | | - |
37 | | - var prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!']; |
38 | | - for (var btnNum = 0; btnNum < prizes.length; btnNum++) { |
39 | | - // for each of our buttons, when the user clicks it... |
40 | | - document.getElementById('btn-' + btnNum).onclick = function() { |
41 | | - // tell her what she's won! |
42 | | - alert(prizes[btnNum]); |
43 | | - }; |
44 | | - } |
45 | | -</script> |
46 | | -``` |
47 | | - |
48 | | -Rewrite to Async: |
49 | | - |
50 | | -```js |
51 | | -1. |
52 | | - |
53 | | -var sum = calculateSum(2, 6); |
54 | | -console.log(sum); |
55 | | - |
56 | | -2. |
57 | | - |
58 | | -var results = $.getJSON('http://myapi.com'); |
59 | | -showResults(results); |
60 | | - |
61 | | -3. |
62 | | - |
63 | | -var sum = calculateSum(2, 6); |
64 | | -if (sum > 8) { |
65 | | - console.log('larger than 8'); |
66 | | -} |
67 | | - |
68 | | -4. |
69 | | - |
70 | | -var data = $.getJSON('http://myapi.com'); |
71 | | -data = data.map(function (x) { return x * 8; }); |
72 | | - |
73 | | -writeDataToFile(data); |
74 | | -``` |
75 | | - |
76 | | - |
77 | | -4. You will need to create an HTML document out of the below snippet to run the below code. A hint - the code is syntactically correct but doesn't do what you would expect. Can you see why and fix it? |
78 | | - |
79 | | -TODO !!! |
80 | | - |
81 | | -Choose two "GET" API endpoints from http://reqres.in |
82 | | -Use $.getJSON to load data from those two endpoints |
83 | | -Display the data on your web page. |
84 | | -It should not matter which endpoint is loaded first, the data should always look the same (you can add "?delay=" after the endpoint to simulate a delay). |
85 | | - |
86 | | -• Create at least 1 issue (bug / feature / code improvement) on another students github repository. Do this in pairs. |
87 | | -• solve the issue proposed by another student in your github repo. More info [here](https://hackyourfuture.slack.com/files/michahell/F31BX1XT6/Merging_a_local_branch_into_master) |
| 2 | +This week you will work on finishing your application so it's actually useful!! |
| 3 | + |
| 4 | +[Read this](https://github.com/HackYourFuture/JavaScript/tree/master/Week8/README.md) |
| 5 | + |
| 6 | +Make this |
| 7 | +- Check if you have met all the requirements from the previous 2 weeks of homework |
| 8 | +- Resolve your Github issues (DO NOT CLOSE AN ISSUE WITHOUT AN EXPLANATION OF BY REFERENCING IT FROM A COMMIT) |
| 9 | +- Make sure you have implemented map, filter, reduce. If you lack inspiration, ask on Slack. |
| 10 | +- Implement promises in your XHR |
| 11 | +- Add polling to your SPA and make sure new info is loaded every 60 seconds. It's probably not a good idea to do a lot of requests every time so maybe just check if a user has new repos. |
| 12 | +- Add a local data structure in which you ONLY store the info your app needs. So all the properties of your object should be used somewhere in your app. Don't reference the response of the XHR anymore, just reference your own data structure. |
| 13 | +- Make sure you only have one request function that accepts a url parameter and a callback function |
| 14 | +- Implement a loader icon like in [my codepen](https://codepen.io/Razpudding/pen/BRGqJw) for each xhr response the user has to wait for. |
| 15 | +- Add correct HTML/CSS |
| 16 | + |
| 17 | +If you get stuck, remember we have Slack and you can ask questions. On Slack there's also the list of repos of other students so you can check how they did it. |
0 commit comments