├── 1_forEach └── README.md ├── 2_map ├── README.md └── spells.js ├── 3_filter ├── README.md └── spells.js ├── 4_reduce ├── README.md └── spells.js └── README.md /1_forEach/README.md: -------------------------------------------------------------------------------- 1 | # Part II: forEach 2 | 3 | Before getting started, make sure that you have a JavaScript console open (like repl.it), so you can complete these exercises. 4 | 5 | ## Exercises 6 | 7 | 1. Refactor the following imperative code to be declarative by using the `forEach()` native array method instead of a `for` loop: 8 | 9 | ```js 10 | var sumImperative = function(array) { 11 | var result = 0; 12 | for (var i = 0; i < array.length; i++) { 13 | result = result + array[i]; 14 | } 15 | return result; 16 | }; 17 | 18 | var sumDeclarative = function(array) { 19 | // your code here 20 | }; 21 | ``` 22 | 23 | 2. Refactor the following imperative code to be declarative by using the `forEach()` native array method instead of a `for` loop: 24 | 25 | ```js 26 | var maxNumImperative = function(array) { 27 | var max = array[0]; 28 | for (var i = 0; i < array.length; i++) { 29 | if (array[i] > max) { 30 | max = array[i]; 31 | } 32 | } 33 | return max; 34 | }; 35 | 36 | var maxNumDeclarative = function(array) { 37 | // your code here 38 | }; 39 | ``` 40 | 41 | 3. Write a function called `min` that find the smallest number in an array of numbers and returns it. 42 | 43 | ```js 44 | var min = function(array) { 45 | // your code here 46 | }; 47 | 48 | min([100, 54, 73, 8, 12, 3]); // => 3 49 | ``` 50 | 51 | 4. Write a function called printNames that takes an array of names and console.logs them: 52 | 53 | ```js 54 | var printNames = function(names) { 55 | // your code here 56 | }; 57 | 58 | printNames(['Tom', 'Jerry', 'Arnold', 'Casper']); 59 | ``` 60 | 61 | ## More Practice 62 | 63 | ## Exercises 64 | 65 | Try to write all of these exercises using .forEach() rather than `for` or `while` loops. 66 | 67 | ### Basic Requirements 68 | 69 | 1. Try the following at a console: 70 | 71 | ```js 72 | "the quick brown fox jumped over the lazy dog".split(" "); 73 | "Hello, world!".split("") 74 | "1,2,3,4,5,6".split(",") 75 | ``` 76 | 77 | What is returned by `split` (You can read more about it 78 | [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)), 79 | and how does it work? 80 | 81 | Use `split` to write a function `longestWord` that takes a string as an 82 | argument and returns the longest word. 83 | 84 | 2. Write a function `remove` that accepts an *array* and an *element*, and 85 | returns an array with all ocurrences of *element* removed. 86 | 87 | ```js 88 | function remove(array, element) { 89 | // your code here 90 | } 91 | remove([1, 3, 6, 2, 3], 3); // => [1, 6, 2] 92 | ``` 93 | 94 | 3. Write a function `evens` that accepts an array as an argument, and returns 95 | an array consisting of all of the *even* numbers in that array. 96 | 97 | ### Exercises Continued 98 | 99 | 1. Write a function called `average` that takes an array of numbers as a 100 | parameter and returns the *average* of those numbers. 101 | 102 | 2. Write a function `shortestWord` that works like `longestWord`, but returns 103 | the *shortest* word instead. 104 | 105 | 3. Write a function `countChar` that takes two arguments: any string, and a 106 | *character* (string of one letter), and returns the number of times that the 107 | character occurs in the string. 108 | 109 | 4. Write a function `evenLengthWords` that takes an array of *strings* as an 110 | argument, and returns an array of just the words that have an even length. 111 | 112 | ### Advanced 113 | 114 | 1. Read about the `join` method on 115 | [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) 116 | and use it to implement a function that accepts a string as an argument and 117 | returns that string *reversed*. 118 | 119 | 2. Write a function `keep` that "keeps" certain elements in an array. The 120 | function will need to take *two* arguments, an array, and something else -- 121 | the second argument will be what is used to determine which elements to keep. 122 | 123 | You should be able to use this function to write `evens`, `evenLengthWords`, 124 | a hypothetical `odds` function, or `oddLengthWords` *without changing the 125 | `keep` function*. 126 | -------------------------------------------------------------------------------- /2_map/README.md: -------------------------------------------------------------------------------- 1 | # Part III: map 2 | 3 | Before getting started, make sure that you have a JavaScript console open (like repl.it), so you can complete these exercises. 4 | 5 | ## Exercises 6 | 7 | 1. Write a function that takes an array of numbers and returns a new array with each number squared: 8 | 9 | ```js 10 | var squared = function(numbers) { 11 | // your code here 12 | }; 13 | 14 | squared([1, 2, 3, 4, 5]); // => [1, 4, 9, 16, 25] 15 | ``` 16 | 17 | 2. Write a function that takes an array of words that are singular and returns an array of the same words pluralized: 18 | 19 | ```js 20 | var pluralize = function(words) { 21 | 22 | }; 23 | 24 | pluralize(['dog', 'cat', 'worm', 'kyle']); // => ['dogs', 'cats', 'worms', 'kyles'] 25 | ``` 26 | 27 | 3. Write a function that takes an array of artist/song objects and returns an array of strings that name the song and say who it is performed by: 28 | 29 | ```js 30 | var songs = [ 31 | { song: 'Phenom', artist: 'Alex Mali' }, 32 | { song: 'Too Deep', artist: 'dvsn' }, 33 | { song: 'Firefly', artist: 'Mura Masa' } 34 | ]; 35 | 36 | var songsBy = function(songs) { 37 | // your code here 38 | }; 39 | 40 | songsBy(songs); // => ['Phenom by Alex Mali', 'Too Deep by dvsn', 'Firefly by Mura Masa'] 41 | ``` 42 | 43 | 44 | 4. Write a function that takes an array of user objects and returns an array of just the users' first names: 45 | 46 | ```js 47 | var users = [ 48 | { firstName: 'Homer', lastName: 'Simpson' }, 49 | { firstName: 'Marge', lastName: 'Simpson' }, 50 | { firstName: 'Bart', lastName: 'Simpson' }, 51 | { firstName: 'Lisa', lastName: 'Simpson' }, 52 | { firstName: 'Maggie', lastName: 'Simpson' } 53 | ]; 54 | 55 | var firstNames = function(users) { 56 | // your code here 57 | }; 58 | 59 | firstNames(users); // => ['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie'] 60 | ``` 61 | 62 | 5. Write a function that takes an array of user objects and returns an array with just the users' full names: 63 | 64 | ```js 65 | var users = [ 66 | { firstName: 'Homer', lastName: 'Simpson' }, 67 | { firstName: 'Marge', lastName: 'Simpson' }, 68 | { firstName: 'Bart', lastName: 'Simpson' }, 69 | { firstName: 'Lisa', lastName: 'Simpson' }, 70 | { firstName: 'Maggie', lastName: 'Simpson' } 71 | ]; 72 | 73 | var fullNames = function(users) { 74 | // your code here 75 | }; 76 | 77 | fullNames(users); // => ['Homer Simpson', 'Marge Simpson', 'Bart Simpson', 'Lisa Simpson', 'Maggie Simpson'] 78 | ``` 79 | 80 | 6. Write a function that takes an array of user objects and returns an array of objects with just the users' full names: 81 | 82 | ```js 83 | var users = [ 84 | { firstName: 'Homer', lastName: 'Simpson' }, 85 | { firstName: 'Marge', lastName: 'Simpson' }, 86 | { firstName: 'Bart', lastName: 'Simpson' }, 87 | { firstName: 'Lisa', lastName: 'Simpson' }, 88 | { firstName: 'Maggie', lastName: 'Simpson' } 89 | ]; 90 | 91 | var fullNameObjects = function(users) { 92 | // your code here 93 | }; 94 | 95 | fullNameObjects(users); // => [{ fullName: 'Homer Simpson' }, { fullName: 'Marge Simpson' }, { fullName: 'Bart Simpson' }, { fullName: 'Lisa Simpson' }, { fullName: 'Maggie Simpson' }] 96 | ``` 97 | 98 | 7. Write a function that takes an array of arrays that contain product information, and returns an array of objects with appropriate keys: 99 | 100 | ```js 101 | var products = [ 102 | ['Dark Chocolate Crunchies', 4.11, 3], 103 | ['Peppermint Poppers', 0.88, 1], 104 | ['Banana Bunches', 2.33, 2] 105 | ] 106 | 107 | var toObject = function(products) { 108 | // your code here 109 | }; 110 | 111 | toObject(products); // => [ 112 | // { name: 'Dark Chocolate Crunchies', price: 4.11, quantity: 3 }, 113 | // { name: 'Peppermint Poppers', price: 0.88, quantity: 1 }, 114 | // { name: 'Banana Bunches', price: 2.33, quantity: 2 } 115 | // ] 116 | ``` 117 | 118 | #### More Practice 119 | 120 | Map is often used to transform a data set to be more readable or usable. Take a look at the array of spell objects in spells.js (in this folder). Using that as your input data set: 121 | 122 | 1. Clean up the data so it is more readable. Write a function that returns an array of spell objects, with each object containing the spell's name, description, and duration: 123 | * Each spell object should look like this: 124 | ```js 125 | { 126 | name: 'spell-name', 127 | description: 'spell-description', 128 | duration: 'spell-duration', 129 | } 130 | ``` 131 | 132 | 2. Write a function that returns an array of spell objects, with each object containing the spell's name and all of the classes and subclasses that can use the spell as subarrays: 133 | * Each spell object should look like this: 134 | ```js 135 | { 136 | name: 'spell-name', 137 | classes: ['class-name-1', 'class-name-2'], 138 | subclasses: ['subclass-name-1', 'subclass-name-2'] 139 | } 140 | ``` -------------------------------------------------------------------------------- /2_map/spells.js: -------------------------------------------------------------------------------- 1 | var spells = [{ 2 | "_id": "58f40b80c9e7ce9f72153112", 3 | "index": 1, 4 | "name": "Acid Arrow", 5 | "desc": [ 6 | "A shimmering green arrow streaks toward a target within range and bursts in a spray of acid. Make a ranged spell attack against the target. On a hit, the target takes 4d4 acid damage immediately and 2d4 acid damage at the end of its next turn. On a miss, the arrow splashes the target with acid for half as much of the initial damage and no damage at the end of its next turn." 7 | ], 8 | "higher_level": [ 9 | "When you cast this spell using a spell slot of 3rd level or higher, the damage (both initial and later) increases by 1d4 for each slot level above 2nd." 10 | ], 11 | "page": "phb 259", 12 | "range": "90 feet", 13 | "components": [ 14 | "V", 15 | "S", 16 | "M" 17 | ], 18 | "material": "Powdered rhubarb leaf and an adder’s stomach.", 19 | "ritual": "no", 20 | "duration": "Instantaneous", 21 | "concentration": "no", 22 | "casting_time": "1 action", 23 | "level": 2, 24 | "school": { 25 | "url": "http://www.dnd5eapi.co/api/magic-schools/5", 26 | "name": "Evocation" 27 | }, 28 | "classes": [ 29 | { 30 | "name": "Wizard", 31 | "url": "http://www.dnd5eapi.co/api/classes/12" 32 | } 33 | ], 34 | "subclasses": [ 35 | { 36 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 37 | "name": "Lore" 38 | }, 39 | { 40 | "url": "http://www.dnd5eapi.co/api/subclasses/4", 41 | "name": "Land" 42 | } 43 | ], 44 | "url": "http://www.dnd5eapi.co/api/spells/1" 45 | }, { 46 | "_id": "58f40b80c9e7ce9f72153113", 47 | "index": 2, 48 | "name": "Acid Splash", 49 | "desc": [ 50 | "You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a dexterity saving throw or take 1d6 acid damage.", 51 | "This spell’s damage increases by 1d6 when you reach 5th level (2d6), 11th level (3d6), and 17th level (4d6)." 52 | ], 53 | "page": "phb 211", 54 | "range": "60 feet", 55 | "components": [ 56 | "V", 57 | "S" 58 | ], 59 | "ritual": "no", 60 | "duration": "Instantaneous", 61 | "concentration": "no", 62 | "casting_time": "1 action", 63 | "level": 0, 64 | "school": { 65 | "url": "http://www.dnd5eapi.co/api/magic-schools/2", 66 | "name": "Conjuration" 67 | }, 68 | "classes": [ 69 | { 70 | "name": "Sorcerer", 71 | "url": "http://www.dnd5eapi.co/api/classes/10" 72 | }, 73 | { 74 | "name": "Wizard", 75 | "url": "http://www.dnd5eapi.co/api/classes/12" 76 | } 77 | ], 78 | "subclasses": [ 79 | { 80 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 81 | "name": "Lore" 82 | } 83 | ], 84 | "url": "http://www.dnd5eapi.co/api/spells/2" 85 | }, { 86 | "_id": "58f40b80c9e7ce9f72153114", 87 | "index": 3, 88 | "name": "Aid", 89 | "desc": [ 90 | "Your spell bolsters your allies with toughness and resolve. Choose up to three creatures within range. Each target’s hit point maximum and current hit points increase by 5 for the duration." 91 | ], 92 | "higher_level": [ 93 | "When you cast this spell using a spell slot of 3rd level or higher, a target’s hit points increase by an additional 5 for each slot level above 2nd." 94 | ], 95 | "page": "phb 211", 96 | "range": "30 feet", 97 | "components": [ 98 | "V", 99 | "S", 100 | "M" 101 | ], 102 | "material": "A tiny strip of white cloth.", 103 | "ritual": "no", 104 | "duration": "8 hours", 105 | "concentration": "no", 106 | "casting_time": "1 action", 107 | "level": 2, 108 | "school": { 109 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 110 | "name": "Abjuration" 111 | }, 112 | "classes": [ 113 | { 114 | "name": "Cleric", 115 | "url": "http://www.dnd5eapi.co/api/classes/3" 116 | }, 117 | { 118 | "name": "Paladin", 119 | "url": "http://www.dnd5eapi.co/api/classes/7" 120 | } 121 | ], 122 | "subclasses": [ 123 | { 124 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 125 | "name": "Lore" 126 | } 127 | ], 128 | "url": "http://www.dnd5eapi.co/api/spells/3" 129 | }, { 130 | "_id": "58f40b80c9e7ce9f72153126", 131 | "index": 4, 132 | "name": "Alarm", 133 | "desc": [ 134 | "You set an alarm against unwanted intrusion. Choose a door, a window, or an area within range that is no larger than a 20-foot cube. Until the spell ends, an alarm alerts you whenever a Tiny or larger creature touches or enters the warded area. When you cast the spell, you can designate creatures that won’t set off the alarm. You also choose whether the alarm is mental or audible.", 135 | "A mental alarm alerts you with a ping in your mind if you are within 1 mile of the warded area. This ping awakens you if you are sleeping.", 136 | "An audible alarm produces the sound of a hand bell for 10 seconds within 60 feet." 137 | ], 138 | "page": "phb 211", 139 | "range": "30 feet", 140 | "components": [ 141 | "V", 142 | "S", 143 | "M" 144 | ], 145 | "material": "A tiny bell and a piece of fine silver wire.", 146 | "ritual": "yes", 147 | "duration": "8 hours", 148 | "concentration": "no", 149 | "casting_time": "1 minute", 150 | "level": 1, 151 | "school": { 152 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 153 | "name": "Abjuration" 154 | }, 155 | "classes": [ 156 | { 157 | "name": "Ranger", 158 | "url": "http://www.dnd5eapi.co/api/classes/8" 159 | }, 160 | { 161 | "name": "Wizard", 162 | "url": "http://www.dnd5eapi.co/api/classes/12" 163 | } 164 | ], 165 | "subclasses": [ 166 | { 167 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 168 | "name": "Lore" 169 | } 170 | ], 171 | "url": "http://www.dnd5eapi.co/api/spells/4" 172 | }] -------------------------------------------------------------------------------- /3_filter/README.md: -------------------------------------------------------------------------------- 1 | # Part IV: filter 2 | 3 | Before getting started, make sure that you have a JavaScript console open (like repl.it), so you can complete these exercises. 4 | 5 | ## Exercises 6 | 7 | 1. Write a function that takes an array of numbers and returns an array of all numbers less than 10: 8 | 9 | ```js 10 | var lessThanTen = function(numbers) { 11 | // your code here 12 | }; 13 | 14 | lessThanTen([1, 5, 12, 18, 94, 3, 16]); // => [1, 5, 3] 15 | ``` 16 | 17 | 2. Write a function that takes an array of numbers and returns an array of just the even numbers: 18 | 19 | ```js 20 | var onlyEvens = function(numbers) { 21 | // your code here 22 | }; 23 | 24 | onlyEvens([25, 16, 12, 99, 8, 37]); // => [16, 12, 8] 25 | ``` 26 | 27 | 3. Write a function that takes an array of strings and returns an array of just the words that have an odd number of characters: 28 | 29 | ```js 30 | var onlyOddWords = function(words) { 31 | // your code here 32 | }; 33 | 34 | onlyOddWords(['hello', 'my', 'name', 'is', 'alexa']); // => ['hello', 'alexa'] 35 | ``` 36 | 37 | 4. Write a function that takes an array of words and returns an array of just the words that are pluralized (end with 's'): 38 | 39 | ```js 40 | var onlyPlural = function(words) { 41 | // your code here 42 | }; 43 | 44 | onlyPlural(['dogs', 'cat', 'humans', 'kyle']); // => ['dogs', 'humans'] 45 | ``` 46 | 47 | 5. Write a function that takes an array of characters and returns an array of just the characters that are superheroes: 48 | ```js 49 | var characters = [ 50 | { character: 'Superman', hero: true }, 51 | { character: 'Sinestro', hero: false }, 52 | { character: 'Wonder Woman', hero: true }, 53 | { character: 'Lex Luthor', hero: false }, 54 | { character: 'Green Lantern', hero: true } 55 | ] 56 | 57 | var isHero = function(chars) { 58 | // your code here 59 | }; 60 | ``` 61 | 62 | #### More Practice 63 | 64 | Filter is used to find certain items in a data set. Take a look at the array of spell objects in spell.js (in this folder). Using that as your input data set: 65 | 66 | 1. Write a function that returns a new array of spells whose level is no higher than 1: 67 | 68 | ```js 69 | var lowLevelSpells = function(spells) { 70 | // your code here 71 | }; 72 | ``` 73 | 74 | 2. Write a function that returns a new array of spells that have `"M"` as one of their components: 75 | 76 | ```js 77 | var componentMSpells = function(spells) { 78 | // your code here 79 | }; 80 | ``` 81 | 82 | 3. Write a function that returns a new array of spells that can be used by the 'Paladin' class: 83 | 84 | ```js 85 | var paladinSpells = function(spells) { 86 | // your code here 87 | }; 88 | ``` 89 | 90 | 4. Write a function that returns a new array of spells that can be cast instantaneously: 91 | 92 | ```js 93 | var instantSpells = function(spells) { 94 | // your code here 95 | }; 96 | ``` 97 | -------------------------------------------------------------------------------- /3_filter/spells.js: -------------------------------------------------------------------------------- 1 | var spells = [{ 2 | "_id": "58f40b80c9e7ce9f72153112", 3 | "index": 1, 4 | "name": "Acid Arrow", 5 | "desc": [ 6 | "A shimmering green arrow streaks toward a target within range and bursts in a spray of acid. Make a ranged spell attack against the target. On a hit, the target takes 4d4 acid damage immediately and 2d4 acid damage at the end of its next turn. On a miss, the arrow splashes the target with acid for half as much of the initial damage and no damage at the end of its next turn." 7 | ], 8 | "higher_level": [ 9 | "When you cast this spell using a spell slot of 3rd level or higher, the damage (both initial and later) increases by 1d4 for each slot level above 2nd." 10 | ], 11 | "page": "phb 259", 12 | "range": "90 feet", 13 | "components": [ 14 | "V", 15 | "S", 16 | "M" 17 | ], 18 | "material": "Powdered rhubarb leaf and an adder’s stomach.", 19 | "ritual": "no", 20 | "duration": "Instantaneous", 21 | "concentration": "no", 22 | "casting_time": "1 action", 23 | "level": 2, 24 | "school": { 25 | "url": "http://www.dnd5eapi.co/api/magic-schools/5", 26 | "name": "Evocation" 27 | }, 28 | "classes": [ 29 | { 30 | "name": "Wizard", 31 | "url": "http://www.dnd5eapi.co/api/classes/12" 32 | } 33 | ], 34 | "subclasses": [ 35 | { 36 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 37 | "name": "Lore" 38 | }, 39 | { 40 | "url": "http://www.dnd5eapi.co/api/subclasses/4", 41 | "name": "Land" 42 | } 43 | ], 44 | "url": "http://www.dnd5eapi.co/api/spells/1" 45 | }, { 46 | "_id": "58f40b80c9e7ce9f72153113", 47 | "index": 2, 48 | "name": "Acid Splash", 49 | "desc": [ 50 | "You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a dexterity saving throw or take 1d6 acid damage.", 51 | "This spell’s damage increases by 1d6 when you reach 5th level (2d6), 11th level (3d6), and 17th level (4d6)." 52 | ], 53 | "page": "phb 211", 54 | "range": "60 feet", 55 | "components": [ 56 | "V", 57 | "S" 58 | ], 59 | "ritual": "no", 60 | "duration": "Instantaneous", 61 | "concentration": "no", 62 | "casting_time": "1 action", 63 | "level": 0, 64 | "school": { 65 | "url": "http://www.dnd5eapi.co/api/magic-schools/2", 66 | "name": "Conjuration" 67 | }, 68 | "classes": [ 69 | { 70 | "name": "Sorcerer", 71 | "url": "http://www.dnd5eapi.co/api/classes/10" 72 | }, 73 | { 74 | "name": "Wizard", 75 | "url": "http://www.dnd5eapi.co/api/classes/12" 76 | } 77 | ], 78 | "subclasses": [ 79 | { 80 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 81 | "name": "Lore" 82 | } 83 | ], 84 | "url": "http://www.dnd5eapi.co/api/spells/2" 85 | }, { 86 | "_id": "58f40b80c9e7ce9f72153114", 87 | "index": 3, 88 | "name": "Aid", 89 | "desc": [ 90 | "Your spell bolsters your allies with toughness and resolve. Choose up to three creatures within range. Each target’s hit point maximum and current hit points increase by 5 for the duration." 91 | ], 92 | "higher_level": [ 93 | "When you cast this spell using a spell slot of 3rd level or higher, a target’s hit points increase by an additional 5 for each slot level above 2nd." 94 | ], 95 | "page": "phb 211", 96 | "range": "30 feet", 97 | "components": [ 98 | "V", 99 | "S", 100 | "M" 101 | ], 102 | "material": "A tiny strip of white cloth.", 103 | "ritual": "no", 104 | "duration": "8 hours", 105 | "concentration": "no", 106 | "casting_time": "1 action", 107 | "level": 2, 108 | "school": { 109 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 110 | "name": "Abjuration" 111 | }, 112 | "classes": [ 113 | { 114 | "name": "Cleric", 115 | "url": "http://www.dnd5eapi.co/api/classes/3" 116 | }, 117 | { 118 | "name": "Paladin", 119 | "url": "http://www.dnd5eapi.co/api/classes/7" 120 | } 121 | ], 122 | "subclasses": [ 123 | { 124 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 125 | "name": "Lore" 126 | } 127 | ], 128 | "url": "http://www.dnd5eapi.co/api/spells/3" 129 | }, { 130 | "_id": "58f40b80c9e7ce9f72153126", 131 | "index": 4, 132 | "name": "Alarm", 133 | "desc": [ 134 | "You set an alarm against unwanted intrusion. Choose a door, a window, or an area within range that is no larger than a 20-foot cube. Until the spell ends, an alarm alerts you whenever a Tiny or larger creature touches or enters the warded area. When you cast the spell, you can designate creatures that won’t set off the alarm. You also choose whether the alarm is mental or audible.", 135 | "A mental alarm alerts you with a ping in your mind if you are within 1 mile of the warded area. This ping awakens you if you are sleeping.", 136 | "An audible alarm produces the sound of a hand bell for 10 seconds within 60 feet." 137 | ], 138 | "page": "phb 211", 139 | "range": "30 feet", 140 | "components": [ 141 | "V", 142 | "S", 143 | "M" 144 | ], 145 | "material": "A tiny bell and a piece of fine silver wire.", 146 | "ritual": "yes", 147 | "duration": "8 hours", 148 | "concentration": "no", 149 | "casting_time": "1 minute", 150 | "level": 1, 151 | "school": { 152 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 153 | "name": "Abjuration" 154 | }, 155 | "classes": [ 156 | { 157 | "name": "Ranger", 158 | "url": "http://www.dnd5eapi.co/api/classes/8" 159 | }, 160 | { 161 | "name": "Wizard", 162 | "url": "http://www.dnd5eapi.co/api/classes/12" 163 | } 164 | ], 165 | "subclasses": [ 166 | { 167 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 168 | "name": "Lore" 169 | } 170 | ], 171 | "url": "http://www.dnd5eapi.co/api/spells/4" 172 | }] -------------------------------------------------------------------------------- /4_reduce/README.md: -------------------------------------------------------------------------------- 1 | # Part V: reduce 2 | 3 | Before getting started, make sure that you have a JavaScript console open (like repl.it), so you can complete these exercises. 4 | 5 | ## Exercises 6 | 7 | Try to implement the following functions using .reduce(). If you are having trouble, try implementing them using more traditional loops and see if you can refactor to .reduce()! 8 | 9 | 1. Write a function that takes an array of numbers and returns the sum of all the numbers: 10 | 11 | ```js 12 | var sum = function(numbers) { 13 | // your code here 14 | }; 15 | 16 | sum([2, 4, 6]); // => 12 17 | ``` 18 | 19 | 2. Write a function that takes an array of numbers and returns the product of all the numbers: 20 | 21 | ```js 22 | var product = function(numbers) { 23 | // your code here 24 | }; 25 | 26 | product([2, 4, 6]); // => 48 27 | ``` 28 | 29 | 3. Write a function that takes an array of words and returns a sentence (single string) with all the element strings concatenated together: 30 | 31 | ```js 32 | var stringConcat = function(strings) { 33 | // your code here 34 | }; 35 | 36 | stringConcat(['Hello', 'my', 'name', 'is', 'Alexandra']); // => 'Hello my name is Alexandra' 37 | ``` 38 | 39 | 4. Write a function that takes an array of users and returns an object with keys that are the users' names and values that are their email addresses: 40 | 41 | ```js 42 | var users = [ 43 | { fullName: 'George Washington', email: 'george@us.gov' }, 44 | { fullName: 'John Adams', email: 'john@us.gov' }, 45 | { fullName: 'Thomas Jefferson', email: 'thomas@us.gov' }, 46 | { fullName: 'James Madison', email: 'james@us.gov' } 47 | ] 48 | 49 | var createEmailObject = function(users) { 50 | // your code 51 | }; 52 | 53 | createEmailObject(users); // => { 54 | // 'George Washington': 'george@us.gov', 55 | // 'John Adams': 'john@us.gov', 56 | // 'Thomas Jefferson': 'thomas@us.gov', 57 | // 'James Madison': 'james@us.gov' 58 | // } 59 | ``` 60 | 61 | ## More Practice 62 | 63 | Take a look at the array of spell objects in spell.js (in this folder). Using that as your input data set: 64 | 65 | 1. Write a function that returns an array of all the spell names. 66 | 67 | ```js 68 | var spellNames = function(spells) { 69 | // your code here 70 | }; 71 | ``` 72 | 73 | 2. Write a function that returns an array of spell objects that have a level higher than 1, with each object holding the spell name and level: 74 | { 75 | name: 'spell-name', 76 | level: 1+ 77 | } 78 | 79 | 80 | ```js 81 | var spellNames = function(spells) { 82 | // your code here 83 | }; 84 | ``` 85 | 86 | ## Challenge Mode 87 | 88 | 1. Implement all of the function exercises for .map() using .reduce() 89 | 90 | 2. Implement all of the function exercises for .filter() using .reduce() -------------------------------------------------------------------------------- /4_reduce/spells.js: -------------------------------------------------------------------------------- 1 | var spells = [{ 2 | "_id": "58f40b80c9e7ce9f72153112", 3 | "index": 1, 4 | "name": "Acid Arrow", 5 | "desc": [ 6 | "A shimmering green arrow streaks toward a target within range and bursts in a spray of acid. Make a ranged spell attack against the target. On a hit, the target takes 4d4 acid damage immediately and 2d4 acid damage at the end of its next turn. On a miss, the arrow splashes the target with acid for half as much of the initial damage and no damage at the end of its next turn." 7 | ], 8 | "higher_level": [ 9 | "When you cast this spell using a spell slot of 3rd level or higher, the damage (both initial and later) increases by 1d4 for each slot level above 2nd." 10 | ], 11 | "page": "phb 259", 12 | "range": "90 feet", 13 | "components": [ 14 | "V", 15 | "S", 16 | "M" 17 | ], 18 | "material": "Powdered rhubarb leaf and an adder’s stomach.", 19 | "ritual": "no", 20 | "duration": "Instantaneous", 21 | "concentration": "no", 22 | "casting_time": "1 action", 23 | "level": 2, 24 | "school": { 25 | "url": "http://www.dnd5eapi.co/api/magic-schools/5", 26 | "name": "Evocation" 27 | }, 28 | "classes": [ 29 | { 30 | "name": "Wizard", 31 | "url": "http://www.dnd5eapi.co/api/classes/12" 32 | } 33 | ], 34 | "subclasses": [ 35 | { 36 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 37 | "name": "Lore" 38 | }, 39 | { 40 | "url": "http://www.dnd5eapi.co/api/subclasses/4", 41 | "name": "Land" 42 | } 43 | ], 44 | "url": "http://www.dnd5eapi.co/api/spells/1" 45 | }, { 46 | "_id": "58f40b80c9e7ce9f72153113", 47 | "index": 2, 48 | "name": "Acid Splash", 49 | "desc": [ 50 | "You hurl a bubble of acid. Choose one creature within range, or choose two creatures within range that are within 5 feet of each other. A target must succeed on a dexterity saving throw or take 1d6 acid damage.", 51 | "This spell’s damage increases by 1d6 when you reach 5th level (2d6), 11th level (3d6), and 17th level (4d6)." 52 | ], 53 | "page": "phb 211", 54 | "range": "60 feet", 55 | "components": [ 56 | "V", 57 | "S" 58 | ], 59 | "ritual": "no", 60 | "duration": "Instantaneous", 61 | "concentration": "no", 62 | "casting_time": "1 action", 63 | "level": 0, 64 | "school": { 65 | "url": "http://www.dnd5eapi.co/api/magic-schools/2", 66 | "name": "Conjuration" 67 | }, 68 | "classes": [ 69 | { 70 | "name": "Sorcerer", 71 | "url": "http://www.dnd5eapi.co/api/classes/10" 72 | }, 73 | { 74 | "name": "Wizard", 75 | "url": "http://www.dnd5eapi.co/api/classes/12" 76 | } 77 | ], 78 | "subclasses": [ 79 | { 80 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 81 | "name": "Lore" 82 | } 83 | ], 84 | "url": "http://www.dnd5eapi.co/api/spells/2" 85 | }, { 86 | "_id": "58f40b80c9e7ce9f72153114", 87 | "index": 3, 88 | "name": "Aid", 89 | "desc": [ 90 | "Your spell bolsters your allies with toughness and resolve. Choose up to three creatures within range. Each target’s hit point maximum and current hit points increase by 5 for the duration." 91 | ], 92 | "higher_level": [ 93 | "When you cast this spell using a spell slot of 3rd level or higher, a target’s hit points increase by an additional 5 for each slot level above 2nd." 94 | ], 95 | "page": "phb 211", 96 | "range": "30 feet", 97 | "components": [ 98 | "V", 99 | "S", 100 | "M" 101 | ], 102 | "material": "A tiny strip of white cloth.", 103 | "ritual": "no", 104 | "duration": "8 hours", 105 | "concentration": "no", 106 | "casting_time": "1 action", 107 | "level": 2, 108 | "school": { 109 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 110 | "name": "Abjuration" 111 | }, 112 | "classes": [ 113 | { 114 | "name": "Cleric", 115 | "url": "http://www.dnd5eapi.co/api/classes/3" 116 | }, 117 | { 118 | "name": "Paladin", 119 | "url": "http://www.dnd5eapi.co/api/classes/7" 120 | } 121 | ], 122 | "subclasses": [ 123 | { 124 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 125 | "name": "Lore" 126 | } 127 | ], 128 | "url": "http://www.dnd5eapi.co/api/spells/3" 129 | }, { 130 | "_id": "58f40b80c9e7ce9f72153126", 131 | "index": 4, 132 | "name": "Alarm", 133 | "desc": [ 134 | "You set an alarm against unwanted intrusion. Choose a door, a window, or an area within range that is no larger than a 20-foot cube. Until the spell ends, an alarm alerts you whenever a Tiny or larger creature touches or enters the warded area. When you cast the spell, you can designate creatures that won’t set off the alarm. You also choose whether the alarm is mental or audible.", 135 | "A mental alarm alerts you with a ping in your mind if you are within 1 mile of the warded area. This ping awakens you if you are sleeping.", 136 | "An audible alarm produces the sound of a hand bell for 10 seconds within 60 feet." 137 | ], 138 | "page": "phb 211", 139 | "range": "30 feet", 140 | "components": [ 141 | "V", 142 | "S", 143 | "M" 144 | ], 145 | "material": "A tiny bell and a piece of fine silver wire.", 146 | "ritual": "yes", 147 | "duration": "8 hours", 148 | "concentration": "no", 149 | "casting_time": "1 minute", 150 | "level": 1, 151 | "school": { 152 | "url": "http://www.dnd5eapi.co/api/magic-schools/1", 153 | "name": "Abjuration" 154 | }, 155 | "classes": [ 156 | { 157 | "name": "Ranger", 158 | "url": "http://www.dnd5eapi.co/api/classes/8" 159 | }, 160 | { 161 | "name": "Wizard", 162 | "url": "http://www.dnd5eapi.co/api/classes/12" 163 | } 164 | ], 165 | "subclasses": [ 166 | { 167 | "url": "http://www.dnd5eapi.co/api/subclasses/2", 168 | "name": "Lore" 169 | } 170 | ], 171 | "url": "http://www.dnd5eapi.co/api/spells/4" 172 | }] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Hack Reactor](https://www.hackreactor.com): Introduction to Functional JavaScript 2 | 3 | Welcome to Hack Reactor's Introduction to Functional JavaScript. You're here to solidify your understanding of functional programming concepts and apply them in JavaScript. In today's session we'll be covering the following topics: 4 | 5 | - Part I: Paradigm & Principles 6 | - Part II: ForEach 7 | - Part III: Map 8 | - Part IV: Filter 9 | - Part V: Reduce 10 | 11 | Each section consists of a lecture followed by a set of self-guided exercises. The self-guided exercises can be found in each section's respective folder in this repository. Before getting started, make sure that you have a JavaScript console open (like repl.it), so you can complete all of the exercises. 12 | 13 | *IMPORTANT*: Completion of this workshop is no guarantee of admission into the Hack Reactor immersive program, nor does it have any influence in the admissions process. 14 | 15 | ## Textbook 16 | 17 | No textbook is required for this workhop. All materials are included in this GitHub repo. 18 | 19 | ## Technical requirements 20 | 21 | Laptop, Google Chrome browser and a text editor. If you do not have a text editor, we recommend Sublime Text, Atom or Visual Studio Code. 22 | 23 | ## Don't forget.. 24 | You should throroughly read all of code in front of you and aim to understand line-by-line what is happening. 25 | 26 | 27 | 28 | ## Slides 29 | 30 | The lesson slides for this workshop can be found [here](https://docs.google.com/presentation/d/e/2PACX-1vQj7Bz4BsXkLt7C-vkUGXrXo7ovtbVWtkTNKeOR-8YrKil5p2xI1EHlbTYjOi9hLv7IEMU7otmm9VX2/pub?start=false&loop=false&delayms=60000). 31 | --------------------------------------------------------------------------------