|
1 | | -# Homework Week 3 (moved over from Week 2) |
2 | | - |
3 | | -1. Create a function that takes two objects as parameters and compares them. You will actually need to write two functions — one that compares with `==` and one that compares with `===`. Remember that objects can have objects inside of them so you'll need to find a way to compare every element of every object (types and values). For example: |
4 | | - |
5 | | - ```js |
6 | | - var obj1 = { |
7 | | - a: 1, |
8 | | - b: 'this is the letter b', |
9 | | - c: { foo: 'what is a foo anyway', |
10 | | - bar: [1,2,3,4] |
11 | | - } |
12 | | - } |
13 | | - |
14 | | - var obj2 = { |
15 | | - a: '1', |
16 | | - b: 'this is the letter b', |
17 | | - c: { foo: 'what is a foo anyway', |
18 | | - bar: [1,2,3,4] |
19 | | - } |
20 | | - } |
21 | | - ``` |
22 | | - |
23 | | - In our example we'll say that `obj1 == obj2` is `true` and `obj1 === obj2` is `false`. Make sure you can see why before you write any code! |
24 | | - |
25 | | - Note: give this exercise your best shot but don’t spend more than, say, one hour on it. |
26 | | - |
27 | | -2. We saw that we can pass functions as arguments to other functions. Your task is to write a function that takes another function as an argument and runs it. |
28 | | - |
29 | | - ```js |
30 | | - function foo(func) { |
31 | | - // What to do here? |
32 | | - } |
33 | | - |
34 | | - function bar() { |
35 | | - console.log('Hello, I am bar!'); |
36 | | - } |
37 | | - |
38 | | - foo(bar); |
39 | | - ``` |
40 | | - |
41 | | - |
42 | | - Write some code to test two arrays for equality using `==` and `===`. Test the following: |
43 | | - |
44 | | - ```js |
45 | | - var x = [1,2,3]; |
46 | | - var y = [1,2,3]; |
47 | | - var z = y; |
48 | | - ``` |
49 | | - What do you think will happen with `x == y`, `x === y` and `z == y` and `z == x`? Prove it! |
50 | | - |
51 | | - > Don't cheat! Seriously - try it first. |
52 | | - |
53 | | - Check out this [Fiddle](http://jsfiddle.net/jimschubert/85M4z/). You need to open your browser’s Developer Tools to see the console output. Press the Run button in the upper right corner to run the code. |
54 | | - |
55 | | - More insights from this [Stack Overflow question](http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript). |
56 | | - |
57 | | - **Some freeCodeCamp challenges:** |
58 | | - |
59 | | -3. [Comparisons with the Logical And Operator](https://www.freecodecamp.com/challenges/comparisons-with-the-logical-and-operator) |
60 | | - |
61 | | -4. [Record Collection](https://www.freecodecamp.com/challenges/record-collection) |
62 | | - |
63 | | -5. [Iterate over Arrays with map](https://www.freecodecamp.com/challenges/iterate-over-arrays-with-map) |
64 | | - |
65 | | -6. We did the following example in class: |
66 | | - |
67 | | - ```js |
68 | | - var o1 = { foo: 'bar' }; |
69 | | - var o2 = { foo: 'bar' }; |
70 | | - var o3 = o2; |
71 | | - ``` |
72 | | - Show that changing `o2` changes `o3` (or not) and changing ~~`o2` changes `o3`~~ `o1` changes `o3`(or not). |
73 | | - |
74 | | - Does the order that you assign (`o3 = o2` or `o2 = o3`) matter? {Jim Cramer: ???} |
75 | | - |
76 | | -### Some further reading: |
77 | | - |
78 | | -Have a look at [The Secret Life of JavaScript Primitives](https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/) |
79 | | - |
80 | | -> ‘Coerce' means to try to change - so coercing `var x = '6'` to number means trying to change the type to number temporarily. |
81 | | -
|
82 | | - |
83 | | -<!-- Original Week 3 homework |
| 1 | +# Homework Week 3 |
84 | 2 |
|
85 | 3 | ## Read: |
86 | 4 | - https://github.com/HackYourFuture/JavaScript/blob/master/Week3/README.md |
@@ -112,5 +30,3 @@ And a custom DOM manipulation challenge :mortar_board: |
112 | 30 | 6. Beautify your html page with css, add sources and alts to each of the images. |
113 | 31 |
|
114 | 32 | 7. __Optional (expert)__ Download book covers for each book, construct a new Object which has as keys the bookId's again, and as value the path to the image source (e.g. `{"harry_potter_blabla": "./img/harry_potter_blabla.jpg", ...}`). Now loop over these entries (_hint: `Object.keys(objectName)` gives you an array containing the keys_). Then write a function which places an image at the corresponding `li` element. Remember that Objects are not ordered, so you cannot guarantee that the first key is the first `li` element. (_Hint: you could give each `li` item an `id` tag by modifying the function you made before_) |
115 | | -
|
116 | | ---> |
0 commit comments