├── README.md ├── LICENSE ├── GTD └── Getting Things Done.markdown ├── JavaScript ├── PassByReferenceandValue.markdown ├── IntroductoryJSArrayObject.markdown └── EqualityOfObjectsAndArraysInJavaScript.markdown ├── React.js ├── WhenDoesReactCallRender.markdown └── IntroductoryReact.markdown ├── PHP └── Introductory Array Exercises.markdown └── HTML+CSS └── Introductory HTML and CSS.markdown /README.md: -------------------------------------------------------------------------------- 1 | Programming-Worksheets 2 | ====================== 3 | 4 | Simple Programming Worksheets for Learning 5 | 6 | 7 | Wrote up the first one as a internal training tool. We may add more (and in other languages) as need permits. 8 | 9 | Contributions/corrections very welcome! 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 David Alan Hjelle 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /GTD/Getting Things Done.markdown: -------------------------------------------------------------------------------- 1 | # Getting Things Done 2 | 3 | A good(?) summary video: https://www.youtube.com/watch?v=NnnaJkKdwjU 4 | 5 | - **"Your mind is for having ideas, not holding them."** 6 | - Have a system for remembering other than your brain. Doesn't matter *what* as much as it matters that you trust it, you use it, and it is fast & easy. 7 | - You don't have to worry about forgetting things. 8 | - You can focus on the one task at hand without being distracted by everything else. 9 | - **Have an "inbox" where you can throw all your ideas, immediately, when you have them.** 10 | - I find paper often works best for this, as switching apps tends to be too slow & distracting. 11 | - You may have multiple inboxes (i.e. paper, digital, etc.) 12 | - Email is just an inbox, not a to-do list. 13 | - Don't organize yet, that's distracting. 14 | - **Regularly go through the inbox to put things in their right place:** 15 | - if you can completely take care of it in 2 minutes, just do it 16 | - some stuff is trash 17 | - some stuff you wait for others for or need to be reminded later—calendar alert or similar 18 | - some stuff just needs to be filed for reference 19 | - otherwise, pick the next actionable thing you can do, put it on your to-do list, and file it away 20 | - **Regularly go through the to-do list, and:** 21 | - remove stuff that doesn't matter 22 | - prioritize what needs to be done next 23 | - some things get delegated to a "someday, maybe" list: good ideas, just not now 24 | - **Do stuff!** 25 | - some tasks require a certain location or tools; do them when you are there 26 | - do small tasks when have small time slot, larger tasks when larger 27 | - do hard tasks when you have lots of energy, easy tasks when you don't 28 | - prioritize by importance 29 | 30 | (adapted from the http://joebuhlig.com/getting-things-done-introduction/ series, adapted from David Allen's Getting Things Done) -------------------------------------------------------------------------------- /JavaScript/PassByReferenceandValue.markdown: -------------------------------------------------------------------------------- 1 | ## Pass by Reference and Value 2 | 3 | > **Note**: The examples here are in JavaScript, but can affect many other languages such as PHP. 4 | 5 | It can be really easy to get confusing results when your functions change ("mutate") the objects that you pass in to them. 6 | 7 | For instance, let's say that you want to write a function that takes in some data of the following form: 8 | 9 | ```javascript 10 | const data = [{ a: 1, b: 2 }, { a: 3, b: 4 }]; 11 | ``` 12 | 13 | It will take each object in the array, calculate the total of its values, and add that total onto the object. In other words, it should return an array like: 14 | 15 | ```javascript 16 | const desired_result = [{ a: 1, b: 2, total: 3 }, { a: 3, b: 4, total: 7 }]; 17 | ``` 18 | 19 | Seems straightforward. 20 | 21 | ```javascript 22 | function totalValues(arr) { 23 | let new_array = []; 24 | for (const obj of arr) { 25 | const values = Object.values(obj); // [1,2] or [3,4] 26 | let total = 0; 27 | for (const el of values) { 28 | total += el; 29 | } 30 | obj.total = total; 31 | new_array.push(obj); 32 | } 33 | return new_array; 34 | } 35 | ``` 36 | 37 | Let's see if it works! 38 | 39 | ```javascript 40 | const totals = totalValues(data); 41 | console.log(JSON.stringify(totals) === JSON.stringify(desired_result)); // true 42 | console.log(totals); // "[{a:1, b:2, total:3}, {a:3, b:4, total:7}]" 43 | ``` 44 | 45 | Looks like it worked perfectly! Hooray! 46 | 47 | But there **is** a lurking problem. What happens if we run the same thing again? 48 | 49 | ```javascript 50 | const totals2 = totalValues(data); 51 | JSON.stringify(totals2) === JSON.stringify(desired_result); // false 52 | ``` 53 | 54 | False?!?! Why did it fail? Let's look more closely… 55 | 56 | ```javascript 57 | console.log(totals2); // "[{a:1, b:2, total:6}, {a:3, b:4, total:14}]" 58 | ``` 59 | 60 | Where are those totals coming from? 61 | 62 | Aaargh!!!! 63 | 64 | What's my data again? 65 | 66 | ```javascript 67 | console.log(data); // "[{a:1, b:2, total:6},{a:3, b:4, total:14}]" 68 | ``` 69 | 70 | Hey! Who put `totals` in my data? 71 | 72 | And therein lies the problem: since `totalValues` **changes** the original objects in `data`, every time we run total, it uses the **most recent** version of `data`, rather than the one originally specified. **Even though we are carefully creating a new array to return!** 73 | 74 | This is where immutable data structures really shine—this problem would not happen. But how do you fix this in plain ol' JavaScript? 75 | 76 | Probably something like: 77 | 78 | ```javascript 79 | new_array.push({ 80 | ...obj, 81 | total 82 | }); 83 | ``` 84 | 85 | -------------------------------------------------------------------------------- /React.js/WhenDoesReactCallRender.markdown: -------------------------------------------------------------------------------- 1 | # When Does React Call Render? 2 | 3 | ## Summary of guidelines 4 | 5 | 1. Keep your `render` function as fast as possible. You can do lots of computation here, but it isn't the place for computationally-heavy processes like indexing of something. 6 | 2. Extend `PureComponent` instead of `Component` if you can. This will do a [shallow comparison](../JavaScript/EqualityOfObjectsAndArraysInJavaScript.markdown) of props and state, and **only** call `render` if they are different. This may require you to design `props` or `state` in a way that complies. 7 | 3. Use `shouldComponentUpdate(nextProps, nextState)` to do the comparison of `props` and `state` yourself (for instance, if they are more complicated than the shallow comparison of `PureComponent` would allow) to avoid re-rendering. Be careful, though, that your check doesn't take more time than just `render`ing would. 8 | 9 | ## When Does React Call Render? 10 | 11 | In general, every time you call `setState` within a component (changing a component's state) or a parent component (changing a component's props). Of course, React may "batch" calls to `setState`, so that multiple `setState` calls could result in a single `render`, but `render` will get called regardless. 12 | 13 | **Additionally**, a component's render will get called **if a parent's `render` method is called**. This can yield `render` getting called much more than you expect. 14 | 15 | If you have a `PureComponent` or you've implemented `shouldComponentUpdate`, then the `setState`→`render` rules are more complicated. 16 | 17 | ## `shouldComponentUpdate(nextProps, nextState)` 18 | 19 | `shouldComponentUpdate` is a lifecycle method that gets called immediately before `render`. If it returns `false`, React will avoid calling the `render` method and assume that the result of `render` is the same as it was last time `render` was called…no change. 20 | 21 | This can be a way to get faster performance. If, for instance, you know your `render` function only depends on `this.props.x`, which is a string, you can implement something like: 22 | 23 | ```javascript 24 | shouldComponentUpdate(nextProps, nextState) { 25 | return nextProps.x !== this.props.x; 26 | } 27 | ``` 28 | 29 | to make sure we re-render **only** if the prop we care about has changed, and **not** if any other props or state have changed, and **not** just because the parent component's `render` method was called. 30 | 31 | Be aware, of course, that if you implement this function incorrectly, you can easily **not** render when you needed to, and get a UI that doesn't respond properly. 32 | 33 | ## `PureComponent` 34 | 35 | Probably the most common `shouldComponentUpdate` implementation does a shallow object comparison of props and state: if your state and props are simple and not complicated/nested data structures, this will work fine to prevent unnecessary re-renders. So, React provides an implementation of this out-of-the-box: extend `PureComponent` instead of `Component` and you'll be on your way! 36 | 37 | -------------------------------------------------------------------------------- /JavaScript/IntroductoryJSArrayObject.markdown: -------------------------------------------------------------------------------- 1 | # Array Object Exercises (JavaScript) 2 | 3 | ## Questions 4 | 5 | 1. If you have an array `var a = [ 0, 1, 2, 3, 4 ];`, how do you extract the value 3 from the array? 6 | 7 | 2. If you have the following object, how do you extract the value 3 from the object? 8 | 9 | ``` 10 | var a = { 11 | "zero": 0, 12 | "one": 1, 13 | "two": 2, 14 | "three": 3, 15 | "four": 4 16 | }; 17 | ``` 18 | 19 | 3. If you have the following array, how do you extract the value 3 from the array? 20 | 21 | ``` 22 | var a = [ 23 | [ 24 | 0, 25 | 1 26 | ], 27 | [ 28 | 2, 29 | [ 30 | 3 31 | ] 32 | ] 33 | ]; 34 | ``` 35 | 36 | 4. If you have the following object, how do you extract the value 3 from the object? 37 | 38 | ``` 39 | var a = { 40 | "a": { 41 | "b": 0, 42 | "c": 1 43 | }, 44 | "b": { 45 | "e": 2, 46 | "o": { 47 | "b": 3 48 | } 49 | } 50 | }; 51 | ``` 52 | 53 | 5. Create a new array with each comma-separated value as its own array element from the string `var a = "a,b,c,d,e,f"`. 54 | 55 | 6. With the result array from `5`, create a new object where each element is *both* key *and* value. In other words, the result should be: 56 | 57 | ``` 58 | { 59 | "a": "a", 60 | "b": "b", 61 | "c": "c", 62 | "d": "d", 63 | "e": "e" 64 | } 65 | ``` 66 | 67 | 7. You have two objects like the following. One contains field labels, the other contains field values. Write a program to output the third object. 68 | 69 | ``` 70 | var keys = { 71 | "field1": "first", 72 | "field2": "second", 73 | "field3": "third" 74 | }; 75 | var values = { 76 | "field1value": "dinosaur", 77 | "field2value": "pig", 78 | "field3value": "platypus" 79 | }; 80 | 81 | // want to output 82 | var keysAndValues = { 83 | "first": "dinosaur", 84 | "second": "pig", 85 | "third": "platypus" 86 | }; 87 | ``` 88 | 89 | 8. You have an array of transactions, each of which has a debit and credit amount. Find the absolute value of the transaction amount (i.e. `abs( debit - credit )`) and add it as a new `key=>value` pair to each transaction. 90 | 91 | ``` 92 | var transactions = [ { 93 | "debit": 2, 94 | "credit": 3 95 | }, { 96 | "debit": 15, 97 | "credit": 14 98 | } ]; 99 | 100 | // outputs 101 | transactions = [ { 102 | "debit": 2, 103 | "credit": 3, 104 | "amount": 1 105 | }, { 106 | "debit": 15, 107 | "credit": 14, 108 | "amount": 1 109 | } ]; 110 | ``` 111 | 112 | 9. Find the sum of this array of numbers `var a = [ 0, 1, 2, 3, 4, 5, 6 ];`. 113 | 114 | ## Sample Answers (others may be correct :-D) 115 | 116 | 1. `a[ 3 ];` 117 | 118 | 2. `a.three;` 119 | 120 | 3. `a[1][1][0];` 121 | 122 | 4. `a.b.o.b;` 123 | 124 | 5. `a = a.split( "," );` 125 | 126 | 6. Samples: 127 | 128 | ``` 129 | var b = {}; 130 | a.forEach( function( value, index ) { 131 | b[ value ] = value; 132 | } ); 133 | ``` 134 | 135 | **or** 136 | 137 | ``` 138 | var b = {}; 139 | for ( var i = 0; i < a.length; i++ ) { 140 | b[ a[ i ] ] = a[ i ]; 141 | } 142 | ``` 143 | 144 | 7. Samples: 145 | 146 | ``` 147 | keysAndValues = {}; 148 | keysAndValues[ keys.field1 ] = values.field1value; 149 | keysAndValues[ keys.field2 ] = values.field2value; 150 | keysAndValues[ keys.field3 ] = values.field3value; 151 | ``` 152 | 153 | **or** 154 | 155 | ``` 156 | var keysAndValues = {}; 157 | [1, 2, 3].forEach(function(number) { 158 | var key = keys["field" + number]; 159 | var value = values["field" + number + "value"]; 160 | keysAndValues[key] = value; 161 | }); 162 | ``` 163 | 164 | 8. Samples 165 | 166 | ``` 167 | transactions.map( function( transaction ) { 168 | return ( transaction.amount = Math.abs( transaction.debit - transaction.credit ) ); 169 | } ); 170 | 171 | ``` 172 | 173 | **or** 174 | 175 | ``` 176 | transactions.forEach( function( transaction ) { 177 | transaction.amount = Math.abs( transaction.debit - transaction.credit ); 178 | } ); 179 | ``` 180 | 181 | 9. Samples: 182 | 183 | ``` 184 | var sum = a.reduce( function( x, y ) { 185 | return x + y; 186 | }, 0 ); 187 | ``` 188 | 189 | **or** 190 | 191 | ``` 192 | var sum = 0; 193 | a.forEach( function( value ) { 194 | sum += value; 195 | } ); 196 | ``` 197 | 198 | **or** 199 | 200 | ``` 201 | var sum = 0; 202 | for ( var i = 0; i < a.length; i++ ) { 203 | sum += a[ i ]; 204 | } 205 | ``` 206 | -------------------------------------------------------------------------------- /JavaScript/EqualityOfObjectsAndArraysInJavaScript.markdown: -------------------------------------------------------------------------------- 1 | # Equality of Objects and Arrays in JavaScript 2 | 3 | Related to the question of [reference and value in calling functions](./PassByReferenceandValue.markdown) is the question of equality: when are two values `===` to each other? (I'm restricting myself to `===` in JavaScript to simplify things a bit; [you can read up the rules if you want](https://dorey.github.io/JavaScript-Equality-Table/).) 4 | 5 | ```javascript 6 | 1 === 1; // true 7 | [1] === [1]; // false 8 | {a: 1} === {a: 1}; // false 9 | ``` 10 | 11 | Why is this? 12 | 13 | In JavaScript, "primitive" values are compared "by value". That means that strings, numbers, and booleans are all compared by the value that they contain, as in line 1 above. 14 | 15 | ## `===` on Objects and Arrays 16 | 17 | On the other hand, objects and arrays (basically everything else in JavaScript) are compared "by reference." That means that the computer doesn't look at all in the values *contained* in the array, but instead simply looks at the memory location that the data is stored in. If the memory location is the same, they are equal, but if the memory location is different, they are *not* equal. While you know that two objects must be equal if they point to the same memory location, the inverse isn't true at all. 18 | 19 | In other words, since we created **two** separate arrays in line 2 above, they point to two separate memory locations, and so are not equal. 20 | 21 | ## Multiple Names for the Same Data 22 | 23 | This is also confusing in other ways. For instance: 24 | 25 | ```javascript 26 | const a = [1]; 27 | const b = a; 28 | a.push(2); 29 | a === b; // true 30 | console.log(a, b); // [1, 2], [1, 2] 31 | ``` 32 | 33 | Even though it looks like we only `push`ed on to `a`, both `a` and `b` point to the same memory location and are two names for the same thing. So they are `===` equal, and will always contain the same information. 34 | 35 | ## How do I copy an object or array? 36 | 37 | It depends. A couple ideas come to mind: 38 | 39 | - `_.clone` will create a shallow clone. 40 | - `_.cloneDeep` will create a deep clone. 41 | - `for` loops can work (shallow). 42 | - `.map` and friends (on arrays) always returns a new array (shallow). 43 | - `JSON.parse(JSON.stringify(x))` can work in a pinch. A deep clone, but is limited to JSON data structures, and I've no idea how the performance compares. But it is easy. 44 | 45 | ### What's with "deep" and "shallow" clones? 46 | 47 | A "deep" clone is what you'd expect: a complete copy without anything shared with the original data structure. However, this can be slower than a shallow clone for nested data structures. 48 | 49 | A "shallow" clone only clones the first level deep of the data structure. This means that some of the values in the object (or array) might still be shared. A simple example: 50 | 51 | ```javascript 52 | const a = [[1]]; 53 | const b = _.clone(a); 54 | a[0].push(2); 55 | console.log(a, b); // [[1, 2]], [[1, 2]] 56 | ``` 57 | 58 | The first element of `a`, which is *also* an array, is shared, so pushing on to it also affects `b`. A deep clone would keep them entirely separate. 59 | 60 | ```javascript 61 | const a = [[1]]; 62 | const b = _.cloneDeep(a); 63 | a[0].push(2); 64 | console.log(a, b); // [[1, 2]], [[1]] 65 | ``` 66 | 67 | ## How do I see if arrays or objects are equal? 68 | 69 | Just as with the clones, there are "deep" and "shallow" variations. Do you care only about the top level? Do you care about every level deep? For that matter, do you care if the elements in an array are in the same order? Do you care about sets or other JavaScript data structures? 70 | 71 | All that said, a few places to get started: 72 | 73 | - `_.isEqual` will do a deep comparison of a lot of JavaScript data structures. 74 | - A `for…in` or `for…of` loop can do a quick shallow comparison. 75 | - `JSON.stringify(x) === JSON.stringify(y)` can be handy if you don't care about the performance and need a quick test while debugging. 76 | - `Immutable.is` works well for Immutable.js data structures (deep, but I'm not sure of the performance characteristics). 77 | 78 | As with the clone, performance depends on how much data you are comparing. 79 | 80 | Here's an example of a loop to compare two objects shallowly (roughly from [Facebook's implementation](https://github.com/facebook/fbjs/blob/4369c7dfeb1ab0de27feb3e9245f599c8edd6a5c/packages/fbjs/src/core/shallowEqual.js#L34-L67): 81 | 82 | ```javascript 83 | function shallowEqual(objA, objB) { 84 | if (is(objA, objB)) { 85 | return true; 86 | } 87 | 88 | if (typeof objA !== 'object' || objA === null || 89 | typeof objB !== 'object' || objB === null) { 90 | return false; 91 | } 92 | 93 | const keysA = Object.keys(objA); 94 | const keysB = Object.keys(objB); 95 | 96 | if (keysA.length !== keysB.length) { 97 | return false; 98 | } 99 | 100 | // Test for A's keys different from B. 101 | for (let i = 0; i < keysA.length; i++) { 102 | if ( 103 | !hasOwnProperty.call(objB, keysA[i]) || 104 | !(objA[keysA[i]] === objB[keysA[i]]) 105 | ) { 106 | return false; 107 | } 108 | } 109 | 110 | return true; 111 | } 112 | ``` 113 | 114 | -------------------------------------------------------------------------------- /PHP/Introductory Array Exercises.markdown: -------------------------------------------------------------------------------- 1 | # Array Exercises (PHP) 2 | 3 | ## Questions 4 | 5 | 1. If you have an array `$a = array( 0, 1, 2, 3, 4 );`, how do you extract the value 3 from the array? 6 | 7 | 2. If you have an array `$a = array( "zero"=>0, "one"=>1, "two"=>2, "three"=>3, "four"=>4 );`, how do you extract the value 3 from the array? 8 | 9 | 3. If you have the following array, how do you extract the value 3 from the array? 10 | 11 | ``` 12 | $a = array( 13 | array( 14 | 0, 15 | 1 16 | ), 17 | array( 18 | 2, 19 | array( 20 | 3 21 | ) 22 | ) 23 | ); 24 | ``` 25 | 26 | 4. If you have the following array, how do you extract the value 3 from the array? 27 | 28 | ``` 29 | $a = array( 30 | "a"=>array( 31 | "b"=>0, 32 | "c=>1 33 | ), 34 | "b"=>array( 35 | "e"=>2, 36 | "o"=>array( 37 | "b"=>3 38 | ) 39 | ) 40 | ); 41 | ``` 42 | 43 | 5. Create a new array with each comma-separated value as its own array element from the string `$a = "a,b,c,d,e,f"`. 44 | 45 | 6. With the result array from `5`, create a new array where each element is *both* key *and* value. In other words, the result should be: 46 | 47 | ``` 48 | array( 49 | "a"=>"a", 50 | "b"=>"b", 51 | "c"=>"c", 52 | "d"=>"d", 53 | "e"=>"e" 54 | ) 55 | ``` 56 | 57 | 7. You have two arrays like the following. One contains field labels, the other contains field values. Write a program to output the third array. 58 | 59 | ``` 60 | $keys = array( 61 | "field1"=>"first", 62 | "field2"=>"second", 63 | "field3"=>"third" 64 | ); 65 | $values = array( 66 | "field1value"=>"dinosaur", 67 | "field2value"=>"pig", 68 | "field3value"=>"platypus" 69 | ); 70 | // want to output 71 | $keysAndValues = array( 72 | "first"=>"dinosaur", 73 | "second"=>"pig", 74 | "third"=>"platypus" 75 | ); 76 | ``` 77 | 78 | 8. You have an array of transactions, each of which has a debit and credit amount. Find the absolute value of the transaction amount (i.e. `abs( debit - credit )`) and add it as a new `key=>value` pair to each transaction. 79 | 80 | ``` 81 | $transactions = array( 82 | array( 83 | "debit"=>2, 84 | "credit"=>3 85 | ), 86 | array( 87 | "debit"=>15, 88 | "credit"=>14 89 | ) 90 | ); 91 | // outputs 92 | $transactions = array( 93 | array( 94 | "debit"=>2, 95 | "credit"=>3, 96 | "amount"=>1 97 | ), 98 | array( 99 | "debit"=>15, 100 | "credit"=>14, 101 | "amount"=>1 102 | ) 103 | ); 104 | ``` 105 | 106 | 9. Find the sum of this array of numbers `$a = array( 0, 1, 2, 3, 4, 5, 6 );`. 107 | 108 | ## Sample Answers (others may be correct :-D) 109 | 110 | 1. `$a[ 3 ];` 111 | 112 | 2. `$a[ "three" ];` 113 | 114 | 3. `$a[ 1 ][ 1 ][ 0 ];` 115 | 116 | 4. `$a[ "b" ][ "o" ][ "b" ];` 117 | 118 | 5. `$a = explode( ",", $a );` 119 | 120 | 6. Samples: 121 | 122 | ``` 123 | $new_array = array(); 124 | foreach( $a as $element ) { 125 | $new_array[ $element ] = $element; 126 | } 127 | ``` 128 | 129 | **or** 130 | 131 | `array_combine( $a, $a );` 132 | 133 | How would you write your own `array_combine` function? 134 | 135 | 7. Samples: 136 | 137 | ``` 138 | // can you spot the bug? 139 | $keysAndValues = array(); 140 | $keysAndValues[ $keys[ "field1" ] ] = $values[ "field1value" ]; 141 | $keysAndValues[ $keys[ "field2" ] ] = $values[ "field2value" ]; 142 | $keysAndValues[ $keys[ "field3" ] ] = $values[ "field2value" ]; 143 | ``` 144 | 145 | **or** 146 | 147 | ``` 148 | $keysAndValues = array(); 149 | foreach( array( 1, 2, 3 ) as $index ) { 150 | $keysAndValues[ $keys[ "field$index" ] ] = $values[ "field" . $index . "value" ]; 151 | } 152 | ``` 153 | 154 | **or** 155 | 156 | ``` 157 | $keysAndValues = array_combine( array_values( $keys ), array_values( $values ) ); 158 | ``` 159 | 160 | 8. Samples 161 | 162 | ``` 163 | $new_transactions = array(); 164 | foreach( $transactions as $transaction ) { 165 | $new_transaction = $transaction; 166 | $new_transaction[ "amount" ] = abs( $transaction[ "debit" ] - $transaction[ "credit" ] ); 167 | $new_transactions[] = $new_transaction; 168 | } 169 | $transactions = $new_transactions; 170 | ``` 171 | 172 | **or** 173 | 174 | ``` 175 | $transactions = array_map( function( $transaction ) { 176 | $transaction[ "amount" ] = abs( $transaction[ "debit" ] - $transaction[ "credit" ] ); 177 | return $transaction; 178 | }, $transactions ); 179 | ``` 180 | 181 | 9. Samples: 182 | 183 | ``` 184 | $sum = 0; 185 | foreach( $a as $element ) { 186 | $sum += $element; 187 | } 188 | ``` 189 | 190 | **or** 191 | 192 | ``` 193 | $sum = array_reduce( $a, function( $partial_sum, $element ) { 194 | return $partial_sum + $element; 195 | }, 0 ); 196 | ``` 197 | 198 | **or** 199 | 200 | ``` 201 | $sum = array_sum( $a ); 202 | ``` 203 | -------------------------------------------------------------------------------- /HTML+CSS/Introductory HTML and CSS.markdown: -------------------------------------------------------------------------------- 1 | # Introductory HTML and CSS 2 | 3 | This document is an attempt to give some of the main background bits to understanding HTML and CSS. It is far from comprehensive, but aims to be approachable and give enough background to understand more complex topics…later. 4 | 5 | ## Server and Browser 6 | 7 | A very basic overview: 8 | 9 | - The browser sends a **request** for a URL to a server. The URL includes a host, a path, and a query string, and the full request includes a set of HTTP headers. 10 | - The request is sent via the HTTP protocol, which sends basically a text file to the server. 11 | - The first part of the file is [**headers**](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields), which have some metadata about the request. 12 | - This includes information about the browser, timestamps, whether it is a GET, POST, or other request, the format the request expects, etc. 13 | - After a blank line, the rest of the file is any **body** of the request that might exist. 14 | - The **response** is sent by the server in much the same format, with the **body** section containing the HTML page or image data or whatever. 15 | - Note that there is no **necessary** correlation between files on disk and the URL. In other words, `http://example.com/important_file.html` could actually retrieve a file on disk called `/stuff/not_important.html`. Further, there may not even be a file on disk that represents the HTML, if you content is being dynamically generated. It's all up to how the HTTP server is configured! 16 | 17 | Example request/response (from [Wikipedia](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session)) 18 | 19 | ### Request 20 | 21 | ``` 22 | GET /index.html HTTP/1.1 23 | Host: www.example.com 24 | ``` 25 | 26 | ### Response 27 | 28 | ``` 29 | HTTP/1.1 200 OK 30 | Date: Mon, 23 May 2005 22:38:34 GMT 31 | Content-Type: text/html; charset=UTF-8 32 | Content-Encoding: UTF-8 33 | Content-Length: 138 34 | Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT 35 | Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) 36 | ETag: "3f80f-1b6-3e1cb03b" 37 | Accept-Ranges: bytes 38 | Connection: close 39 | 40 | 41 |
42 |text
` or be "self-closing" ``. While the HTML spec is not strict, I'd recommend **closing all tags**, either with a closing tag or by self-closing. 121 | - A few tags cannot have any content, such as the `` might indicate how important the paragraph is). 123 | - Attributes live inside the angle brackets after the tag name, with a label, an equals sign, and a value. 124 | - Some true/false attributes *only* need the label of the attribute. 125 | - Values technically don't need quotes, but it's simpler. Double or single is fine. 126 | - **Text** usually just shows up. It *should* probably be wrapped inside a tag, but doesn't have to be. 127 | - Spaces are collapsed—HTML enforces single spaces between words and sentences. 128 | - Use [HTML entities](https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references) for `<` (`<`) or `>` (`>`). 129 | - Browsers are (somewhat unfortunately) very forgiving and will try guess what tags you *meant* to have. Validation tools are recommended, especially ones built right in to your editor. 130 | 131 | ### Default Styles 132 | 133 | It isn't true for every tag (with the form elements being the most notable exception), but it is often true that the only difference between one tag and another is the default styling that the browser provides. 134 | 135 | (You'll likely want to include [Normalize.css](https://necolas.github.io/normalize.css/) to try make the browser default styles match across browsers. Bootstrap already includes this by default.) 136 | 137 | ### Browser Development Tools 138 | 139 | While all major browser include quality developer's tools, I will focus on [Chrome's DevTools](https://developers.google.com/web/tools/chrome-devtools/). Each browser's tools has their own strengths and weaknesses, and it isn't too uncommon to use more than one set when debugging certain issues. 140 | 141 | - Right-click on a page element and choose "Inspect Element". This will open the dev tools in most browsers, and focus that particular element in the element tree. 142 | - This is a live-updating representation of the HTML on your page, including any changes that JavaScript has made. 143 | - You can see the tree structure of the HTML DOM. 144 | - You can highlight any given element to see information about the CSS rules applied to that node, JavaScript event listeners, and more. 145 | - You can also live-edit the page—though of course your edits don't get saved. (Well, if you are working on something you have access to, there is theoretically a way for the changes to get saved…but I've never set that up.) 146 | - Get applied style information. 147 | - When you have an element focused in the Elements pane, you can look at the currently applied styles. 148 | - This shows **all** styles, organized with the most specific ones at the top, from any location that applies to this element, including the browser's default "user agent stylesheet". 149 | - There is also a graphic showing a bit of the box model, the margin, border, and padding applied to the element. 150 | - Get computed style information. 151 | - Sometimes, even when you see all the applied styles, you just want to know what the **end result** of applying all those styles was. That's what the "Computed" tab is for. 152 | - There is sometimes a disclosure triangle to the left of the properties, allowing you to trace back which CSS rules were used to make this computation. 153 | - [Lots more in the Chrome documentation](https://developer.chrome.com/devtools/docs/elements-styles). 154 | 155 | ### Useful `
` Tags 156 | 157 | Note that the `` tag is has CSS `display: none` by default…but even that can be overridden! 158 | 159 | - `