|
1 | | -## Assignment week 2 |
| 1 | +## Homework Week 2 |
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: |
| 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 | 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] |
| 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 | + } |
11 | 12 | } |
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] |
| 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 | + } |
19 | 20 | } |
20 | | -} |
21 | | -``` |
| 21 | + ``` |
22 | 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! |
| 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. |
24 | 26 |
|
25 | 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. |
26 | 28 |
|
27 | | -```js |
28 | | -function foo(func) { |
29 | | - // What to do here? |
30 | | -} |
| 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 | + ``` |
31 | 40 |
|
32 | | -function bar() { |
33 | | - console.log('Hello, I am bar!'); |
34 | | -} |
35 | 41 |
|
36 | | -foo(bar); |
37 | | -``` |
| 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. |
38 | 54 |
|
| 55 | + More insights from this [Stack Overflow question](http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript). |
39 | 56 |
|
40 | | -Write some code to test two arrays for equality using == and ===. Test the following: |
41 | | -var x = [1,2,3]; |
42 | | -var y = [1,2,3]; |
43 | | -var z = y; |
44 | | -What do you think will happen with x == y, x === y and z == y and z == x? Prove it! |
45 | | -Don't cheat! Seriously - try it first. |
46 | | -http://jsfiddle.net/jimschubert/85M4z/ |
47 | | -http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript |
| 57 | + **Some freeCodeCamp challenges:** |
48 | 58 |
|
49 | | -3. https://www.freecodecamp.com/challenges/comparisons-with-the-logical-and-operator |
| 59 | +3. [Comparisons with the Logical And Operator](https://www.freecodecamp.com/challenges/comparisons-with-the-logical-and-operator) |
50 | 60 |
|
51 | | -4. https://www.freecodecamp.com/challenges/record-collection |
| 61 | +4. [Record Collection](https://www.freecodecamp.com/challenges/record-collection) |
52 | 62 |
|
53 | | -5. https://www.freecodecamp.com/challenges/iterate-over-arrays-with-map |
| 63 | +5. [Iterate over Arrays with map](https://www.freecodecamp.com/challenges/iterate-over-arrays-with-map) |
54 | 64 |
|
55 | 65 | 6. We did the following example in class: |
56 | 66 |
|
57 | | -```js |
58 | | - var o1 = { foo: 'bar' }; |
59 | | - var o2 = { foo: 'bar' }; |
60 | | - var o3 = o2; |
61 | | -``` |
62 | | -Show that changing o2 changes o3 (or not) and changing o2 changes o3 (or not). |
63 | | - |
64 | | -Does the order that you assign (o3 = o2 or o2 = o3) matter? |
| 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: ???} |
65 | 75 |
|
66 | 76 | ### Some further reading: |
67 | | -Have a look at https://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ |
| 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/) |
68 | 79 |
|
69 | | -'Coerce' means to try to change - so coercing `var x = '6'` to number means means trying to change the type to number temporarily. |
| 80 | +> ‘Coerce' means to try to change - so coercing `var x = '6'` to number means trying to change the type to number temporarily. |
0 commit comments