Skip to content

Latest commit

 

History

History
111 lines (76 loc) · 3.69 KB

File metadata and controls

111 lines (76 loc) · 3.69 KB

Homework Week 9

Read:

Make:

  1. Let's continue to learn a little more about scope and Closures.

  2. What will be the output of the following code - and more importantly - WHY?

for (var i = 0; i < 3; i++) {
      setTimeout(function() { alert(i); }, 1000 + i);
}
  1. Write a function that would allow you to do this:
var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
  1. 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?

Don't cheat - but if you get stuck ... http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example

<button id="btn-0">Button 1!</button>
<button id="btn-1">Button 2!</button>
<button id="btn-2">Button 3!</button>

<script type="text/javascript">
    
    var prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!'];
    for (var btnNum = 0; btnNum < prizes.length; btnNum++) {
        // for each of our buttons, when the user clicks it...
        document.getElementById('btn-' + btnNum).onclick = function() {
            // tell her what she's won!
            alert(prizes[btnNum]);
        };
    }
</script>

Rewrite to Async:

1.

var sum = calculateSum(2, 6);
console.log(sum);

2.

var results = $.getJSON('http://myapi.com');
showResults(results);

3.

var sum = calculateSum(2, 6);
if (sum > 8) {
    console.log('larger than 8');
}

4.

var data = $.getJSON('http://myapi.com');
data = data.map(function (x) { return x * 8; });

writeDataToFile(data);
  1. 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?

TODO !!!

Choose two "GET" API endpoints from http://reqres.in Use $.getJSON to load data from those two endpoints Display the data on your web page. 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).

• Create at least 1 issue (bug / feature / code improvement) on another students github repository. Do this in pairs. • solve the issue proposed by another student in your github repo. More info here

Optional homework

TicTacToe game

https://github.com/HackYourFuture/TicTacToeTDD
https://github.com/HackYourFuture/OOP-Student-and-Teacher

rewatch the Hangouts session here: https://www.youtube.com/watch?v=oc9ogCJz9rYs

Upload your homework in your "hyf-javascript3" Github repository. Make sure to create a new folder "week3" first. Upload your homework files inside the week3 folder and write a description for this “commit”. Your hyf-javascript3/week3 should now contain all your homework files. Place the link to your repository folder in Trello.