Skip to content

Latest commit

 

History

History
101 lines (69 loc) · 3.49 KB

File metadata and controls

101 lines (69 loc) · 3.49 KB

Homework Week 9

Step 0: Read

Read:

Step 1: Scope and Closures

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

  1. 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:
let 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">
    
    let prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!'];
    for (let 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>
  1. Rewrite the code below to Async:
1.

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

2.

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

3.

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

4.

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

writeDataToFile(data);

Step 2: Feedback

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

Step 3: Pair programming homework

You will complete the TicTacToe game we started building in class. The code we build in class is available here: https://github.com/HackYourFuture/TicTacToeTDD. One of your teammates should fork the above repo. Work in this repository together so you can see who wrote which code. Make a Pull Request to hand in this part of the homework.

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.