├── .DS_Store ├── 00_expressions_variables ├── .DS_Store ├── exercises │ ├── A_executing_code_exercise │ │ └── README.md │ ├── B_expressions_exercise │ │ └── README.md │ ├── C_variables_exercise │ │ └── README.md │ └── D_string_indexing_and_methods_exercise │ │ └── README.md └── solutions │ ├── A_executing_code_solution │ ├── age.js │ ├── greeting.js │ ├── thirsty.js │ └── whoami.js │ ├── B_expressions_solution │ ├── boolean-expressions.js │ ├── comparisons.js │ ├── number-expressions.js │ └── string-expressions.js │ ├── C_variables_solution │ ├── four.js │ ├── one.js │ ├── three.js │ ├── two.js │ └── zero.js │ └── D_string_indexing_and_methods_solution │ ├── a-tedious-task.js │ ├── dare-to-decipher.js │ ├── proper-patterns.js │ └── small-string-snippets.js ├── 01_conditionals ├── exercises │ └── A_conditionals_exercise │ │ └── README.md └── solutions │ └── A_conditionals_solution │ ├── one.js │ ├── three.js │ ├── two.js │ └── zero.js ├── 02_functions ├── exercises │ ├── A_functions_exercise │ │ └── README.md │ ├── B_functions_exercise │ │ └── README.md │ └── C_functions_exercise │ │ └── README.md └── solutions │ ├── A_functions_solution │ ├── average.js │ ├── ends-with-t.js │ ├── half.js │ ├── is-div-by-4.js │ ├── is-long.js │ ├── keep-it-quiet.js │ ├── snippet-0-1.js │ ├── snippet-0-2.js │ ├── snippet-0-3.js │ └── snippet-0-4.js │ ├── B_functions_solution │ ├── ends-in-ly.js │ ├── funny-sound.js │ ├── longer.js │ ├── one-or-none.js │ ├── parity.js │ ├── starts-with-r.js │ ├── string-size.js │ └── wacky-word.js │ └── C_functions_solution │ ├── average-of-four.js │ ├── case-changer.js │ ├── contains.js │ ├── divisible.js │ ├── in-range.js │ ├── larger.js │ └── number-change.js ├── 03_loops ├── exercises │ ├── A_loops_exercise │ │ └── README.md │ ├── B_loops_exercise │ │ └── README.md │ └── C_loops_exercise │ │ └── README.md └── solutions │ ├── A_loops_solution │ ├── count-up.js │ ├── evens.js │ ├── min-to-max.js │ ├── one-to-four.js │ ├── snippet-1.js │ ├── snippet-2.js │ ├── snippet-3.js │ ├── snippet-4.js │ ├── snippet-5.js │ └── string-iterate.js │ ├── B_loops_solution │ ├── div-by-either.js │ ├── five-multiples-of.js │ ├── no-ohs.js │ ├── odd-sum.js │ ├── product-up-to.js │ ├── string-repeater.js │ └── sum-up-to.js │ └── C_loops_solution │ ├── censor-e.js │ ├── divisible-range.js │ ├── fizz-buzz.js │ ├── raise-power.js │ ├── remove-capitals.js │ └── reverse-iterate.js ├── 04_arrays ├── .DS_Store ├── exercises │ ├── A_arrays_exercise │ │ └── README.md │ ├── B_arrays_exercise │ │ └── README.md │ └── C_arrays_exercise │ │ └── README.md └── solutions │ ├── A_arrays_solution │ ├── bleep-vowels.js │ ├── divisors.js │ ├── filter-long-words.js │ ├── num-odds.js │ ├── stay-positive.js │ ├── strings-to-lengths.js │ └── total.js │ ├── B_arrays_solution │ ├── choose-divisibles.js │ ├── make-acronym.js │ ├── maximum.js │ ├── reverse-array.js │ ├── word-count.js │ └── your-average-function.js │ └── C_arrays_solution │ ├── alternating-caps.js │ ├── common-elements.js │ ├── lengthiest-word.js │ ├── number-range.js │ └── remove-short-words.js ├── 05_nesting ├── .DS_Store ├── exercises │ ├── .DS_Store │ ├── A_nested_loops_exercise │ │ └── README.md │ ├── B_two_dimensional_arrays_exercise │ │ └── README.md │ └── C_more_problems_exercise │ │ └── README.md └── solutions │ ├── .DS_Store │ ├── A_nested_loops_solution │ ├── pair-print.js │ ├── print-combinations.js │ ├── snippet-1.js │ ├── snippet-2.js │ ├── snippet-3.js │ ├── snippet-4.js │ ├── snippet-5.js │ └── two-sum.js │ ├── B_two_dimensional_arrays_solution │ ├── make-matrix.js │ ├── print-2d.js │ ├── total-product.js │ ├── two-sum-pairs.js │ └── zipper.js │ └── C_more_problems_solution │ ├── remove-dupes.js │ ├── remove-first-vowel.js │ ├── remove-vowels.js │ ├── shorten-long-words.js │ └── spam.js ├── 06_decomposition_pattern ├── A_decomposing_problems_exercise │ └── README.md ├── B_decomposing_problems_exercise │ └── README.md └── C_decomposing_problems_exercise │ └── README.md ├── 07_objects ├── A_objects_exercise │ └── README.md ├── B_objects_exercise │ └── README.md └── C_objects_exercise │ └── README.md ├── 08_higher_order_functions ├── A_native_callback_methods_exercise │ └── README.md └── B_native_callback_methods_exercise │ └── README.md └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/.DS_Store -------------------------------------------------------------------------------- /00_expressions_variables/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/00_expressions_variables/.DS_Store -------------------------------------------------------------------------------- /00_expressions_variables/exercises/A_executing_code_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Write, Run, Repeat 2 | 3 | You won't become a strong programmer with "one secret trick" or "however-many expert tips". No 4 | shortcuts. Just write code, run it, do it again. We'll be creating and executing hundreds of 5 | programs in this course. Let's bang out the first four. 6 | 7 | Before you start this exercise, make sure you have watched the lecture first. 8 | 9 | + [Lecture](https://youtu.be/U-679Jd3fmQ) 10 | 11 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 12 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 13 | 14 | + [Walkthrough](https://youtu.be/f7CZUGFdhfg) 15 | 16 | Let's get to work. 17 | 18 | -AZ 19 | 20 | ## 0. Follow my lead. 21 | 22 | 1. If you already have VSCode open, close it. 23 | 2. On your computer's Desktop, create a new folder named `my-first-programs`. 24 | 3. Open the `my-first-programs` folder in VSCode 25 | 4. Use VSCode to create a file named `greeting.js` inside of `my-first-programs`. 26 | 5. Inside of `greeting.js`, write the single line of code `console.log('hello world');`. This will 27 | cause the program to print `hello world` when you execute it later. 28 | 6. Save the `greeting.js` file using the keyboard shortcut `cmd + s` or `ctrl + s`. 29 | 7. Open the terminal and enter the command `node greeting.js` to execute your code. **You should see 30 | `hello world` printed out.** 31 | 32 | ## 1. Still following? 33 | 34 | 1. Inside the `my-first-programs` folder, create a new file named `whoami.js`. 35 | 2. Inside of `whoami.js` use `console.log` to print out your first name. Don't forget to put 36 | quotation marks (`'`) around your name and end the line with a semicolon (`;`). 37 | 3. Save `whoami.js` and execute it in your terminal using the `node`. **You should see your name 38 | printed out.** 39 | 40 | ## 2. I'll follow you. 41 | 42 | Start from scratch. Close VSCode if you have it open. Use VSCode to write a program named 43 | `thirsty.js` inside of the `my-first-programs` folder. The program should print out your favorite 44 | cold beverage. Execute `thirsty.js` in your terminal. 45 | 46 | ## 3. Beginner's luck; show me it again. 47 | 48 | That seems suspicious. Write a program named `age.js` that prints out your age. Run it. 49 | 50 | ## Walkthrough 51 | 52 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 53 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 54 | Watch it in 1.5x speed if you like. 55 | -------------------------------------------------------------------------------- /00_expressions_variables/exercises/B_expressions_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Express Yourself 2 | 3 | _Expressions_ are the building blocks of programs. We can combine _values_ with _operators_ to 4 | compose an expression. An _expression_ evaluates to some _value_. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture Part 1](https://youtu.be/NORcxNPVtg4) 9 | + [Lecture Part 2](https://youtu.be/1y9DCzWYons) 10 | 11 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 12 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 13 | 14 | + [Walkthrough](https://youtu.be/oMHa_c21sCw) 15 | 16 | The fundamentals of any skill aren't particularly glamorous. We'll learn the "cool" stuff when you 17 | are ready. Crawl. Walk. Run. 18 | 19 | -AZ 20 | 21 | ## 0. Hope you didn't forget. 22 | 23 | Create a new folder named `expressions-exercise`. Open it in VSCode. Create four files within this 24 | folder: `string-expressions.js`, `number-expressions.js`, `boolean-expressions.js`, 25 | `comparisons.js`. 26 | 27 | After creating these files, your folder structure should look like this: 28 | 29 | ```plaintext 30 | expressions-exercise/ 31 | ├── string-expressions.js 32 | ├── number-expressions.js 33 | ├── boolean-expressions.js 34 | └── comparisons.js 35 | ``` 36 | 37 | ## 1. Stringing together some expressions. 38 | 39 | Make a mental prediction of what the following expressions will print: 40 | 41 | ```javascript 42 | console.log("river" + "town"); 43 | console.log("cat" + "dog"); 44 | console.log("New" + " York"); 45 | console.log("runner's knee" + "!"); 46 | console.log("man" + "bear" + "pig"); 47 | ``` 48 | 49 | To check your prediction, write the code into `string-expressions.js` and run it in your terminal 50 | with `node string-expressions.js`. Don't copy and paste. Type everything manually. You should do 51 | this line by line. This means you should type the first expression, run it. Add the second 52 | expression, run it, and so on. Don't try to type all five lines and then run it all at once. You 53 | will find it hard to pinpoint any typos if you do that. 54 | 55 | ## 2. Don't enjoy math? Too bad. 56 | 57 | Make predictions for what these number expressions evaluate to. Test your predictions by writing the 58 | code to `number-expressions.js` and running it: 59 | 60 | ```js 61 | console.log(2 + 3); 62 | console.log(10 - 15); 63 | console.log(4 + 1 - 5); 64 | console.log(4 * 3); 65 | 66 | console.log(7 / 2); 67 | console.log(4 + 2 * 3); 68 | console.log((4 + 2) * 3); 69 | console.log(5 % 2); 70 | 71 | console.log(6 % 2); 72 | console.log(7 % 2); 73 | console.log(8 % 2); 74 | console.log(19 % 8); 75 | 76 | console.log(24 % 8); 77 | console.log(7 % 4); 78 | console.log(4 % 7); 79 | console.log(5 + (10 % 5)); 80 | console.log((5 + 10) % 5); 81 | ``` 82 | 83 | ## 3. Shoutout to George Boole. 84 | 85 | Make predictions for what these boolean expressions evaluate to. Test your predictions by writing 86 | the code to `boolean-expressions.js` and running it: 87 | 88 | ```js 89 | console.log(false); 90 | console.log(!true); 91 | console.log(!false); 92 | console.log(!!true); 93 | 94 | console.log(false && false); 95 | console.log(false && true); 96 | console.log(true && false); 97 | console.log(true && true); 98 | 99 | console.log(false || false); 100 | console.log(false || true); 101 | console.log(true || false); 102 | console.log(true || true); 103 | 104 | console.log(!false || false); 105 | console.log(false || (true && true)); 106 | console.log(false || !(true && true)); 107 | console.log(!true && (false || true)); 108 | ``` 109 | 110 | ## 4. Drawing a Comparison. 111 | 112 | Make predictions for what these comparison expressions evaluate to. Test your predictions by writing 113 | the code to `comparisons.js` and running it: 114 | 115 | ```js 116 | console.log(true === false); 117 | console.log(false === false); 118 | console.log(false !== true); 119 | console.log(!true === false); 120 | 121 | console.log(2 + 3 === 5); 122 | console.log(4 < 0); 123 | console.log(10 >= 10); 124 | console.log(10.3 >= 10); 125 | 126 | console.log(100 / 2 === 50); 127 | console.log(100 % 2 === 0); 128 | console.log(11 % 2 === 0); 129 | console.log(7.0 === 7); 130 | 131 | console.log(13 % 5 > 0); 132 | console.log("potato" === "potato"); 133 | console.log("Tomato" === "tomato"); 134 | console.log("42" === 42); 135 | console.log(5 > 3 && 1 === 0); 136 | ``` 137 | 138 | ## Walkthrough 139 | 140 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 141 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 142 | Watch it in 1.5x speed if you like. 143 | -------------------------------------------------------------------------------- /00_expressions_variables/exercises/C_variables_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Very Able 2 | 3 | _Variables_ allow programmers to store values to be used later in their code. The value that a 4 | variable contains can change throughout the runtime of a program. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture](https://youtu.be/lmXRLpoyPic) 9 | 10 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 11 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 12 | 13 | + [Walkthrough](https://youtu.be/T_xE-x0Eyqk) 14 | 15 | Ready? 16 | 17 | -AZ 18 | 19 | ## 0. Do you speak the language? 20 | 21 | Create a folder named `variables_exercise`. Create a file named `zero.js` inside of it. Predict what 22 | the `console.log`s in the code below will print out: 23 | 24 | ```js 25 | let location = "Brooklyn, " + "NY"; 26 | console.log(location); 27 | 28 | let quantity = 4 * 5 + 1; 29 | console.log(quantity); 30 | ``` 31 | 32 | Test your predictions by typing the code into the `zero.js` file; don't copy and paste. Run it to 33 | check your guess. 34 | 35 | ## 1. What's changed? What hasn't? 36 | 37 | Create a new file named `one.js` inside of the `variables_exercise` folder. Predict what the 38 | `console.log`s in the code below will print out: 39 | 40 | ```js 41 | let word = "bye"; 42 | console.log(word + " felicia"); 43 | console.log(word); 44 | 45 | let num = 10; 46 | num = num * 2; 47 | console.log(num); 48 | 49 | let bottlesOfBeer = 99; 50 | let around = bottlesOfBeer - 1; 51 | console.log(around); 52 | console.log(bottlesOfBeer); 53 | ``` 54 | 55 | Test your predictions by typing the code into the `one.js` file. Run it. 56 | 57 | ## 2. How about this? 58 | 59 | Create a new file named `two.js` inside of the `variables_exercise` folder. Predict what the 60 | `console.log`s in the code below will print out: 61 | 62 | ```js 63 | let apple; 64 | console.log(apple); 65 | 66 | apple = 5; 67 | console.log(apple); 68 | 69 | apple + 1; 70 | console.log(apple); 71 | 72 | apple += 1; 73 | console.log(apple); 74 | 75 | let banana = apple; 76 | console.log(banana); 77 | 78 | banana = banana / 2; 79 | console.log(banana); 80 | 81 | console.log(apple); 82 | ``` 83 | 84 | Test your predictions by typing the code into the `two.js` file. Run it. 85 | 86 | ## 3. Did you forget about modulo? 87 | 88 | Create a new file named `three.js` inside of the `variables_exercise` folder. Predict what the 89 | `console.log`s in the code below will print out: 90 | 91 | ```js 92 | let qty = 15 % 4; 93 | console.log(qty); 94 | 95 | let num = 38 + 3; 96 | num++; 97 | console.log(num + " is a great number"); 98 | 99 | let isNumEven = num % 2 === 0; 100 | console.log(num + " is even? " + isNumEven); 101 | 102 | let isQtyEven = qty % 2 === 0; 103 | console.log(qty + " is even? " + isQtyEven); 104 | ``` 105 | 106 | ## 4. So you can code, but do you have good taste in cinema? 107 | 108 | Create a new file named `four.js` inside of the `variables_exercise` folder. Create two variables: 109 | 110 | - `movie`, assigned to the string title of your favorite movie 111 | - `views`, assigned to the number of times you watched that movie 112 | 113 | When executed, the program should print the message 114 | `I watched about number of times`. 115 | 116 | ## Walkthrough 117 | 118 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 119 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 120 | Watch it in 1.5x speed if you like. 121 | -------------------------------------------------------------------------------- /00_expressions_variables/exercises/D_string_indexing_and_methods_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Intelligent Indexing 2 | 3 | A string consists of one or more characters. An _index_ is a number that corresponds to the position 4 | of a character in a string. The first character of a string has _index 0_, the second character has 5 | _index 1_, and so on. _Indexing_ is a pattern that programmers use often in strings. This pattern 6 | will also appear later in more advanced applications. 7 | 8 | Before you start this exercise, make sure you have watched the lecture first. 9 | 10 | + [Lecture](https://youtu.be/QoeKfd9MYWA) 11 | 12 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 13 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 14 | 15 | + [Walkthrough Part 1](https://youtu.be/lXlNybyHclc) 16 | + [Walkthrough Part 2](https://youtu.be/4sf1uP9OIDI) 17 | 18 | This is where we begin to unlock the basic programming patterns that you'll always be able to rely 19 | on. Study them closely. 20 | 21 | -AZ 22 | 23 | ## 0. Small String Snippets. 24 | 25 | Create a new folder named `string-indexing-and-methods`. Create a file named 26 | `small-string-snippets.js` inside. Predict what the following code will print out: 27 | 28 | ```js 29 | console.log("promenade"[3]); 30 | console.log("tiger"[1]); 31 | console.log("wheel".length); 32 | console.log("wheel".length - 1); 33 | console.log("noMAD".toUpperCase()); 34 | console.log("hey programmers"[2] === "y"); 35 | 36 | console.log("volleyball".length > 20); 37 | console.log("treasure".indexOf("r")); 38 | console.log("treasure".indexOf("e")); 39 | console.log("web"[5]); 40 | console.log("red".indexOf("x")); 41 | console.log("red".indexOf("R")); 42 | ``` 43 | 44 | Verify your predictions by writing the code to the `small-string-snippets.js` file. Don't copy and 45 | paste; type everything out manually. Run the code. 46 | 47 | ## 1. Proper Patterns. 48 | 49 | Inside of the `string-indexing-and-methods` folder create another file named `proper-patterns.js`. 50 | Predict what the following code will print out: 51 | 52 | ```js 53 | let word = "suspension bridge"; 54 | console.log(word[4]); 55 | console.log(word.length > 5 && word[0] === "d"); 56 | console.log(word.length > 5 && word[0] === "s"); 57 | console.log(word.indexOf("o") > -1); 58 | console.log(word.indexOf("z") > -1); 59 | 60 | let str = "foggy"; 61 | console.log(str[2 + 1]); 62 | console.log(str[str.length - 1]); 63 | str = " day"; 64 | console.log(str); 65 | console.log(str.length); 66 | console.log(str.indexOf("ogg")); 67 | ``` 68 | 69 | Type the code. Don't copy and paste. Run it. Check your predictions. 70 | 71 | ## 2. Dare to Decipher? 72 | 73 | Inside of the `string-indexing-and-methods` folder create another file named `dare-to-decipher.js`. 74 | Predict what the following code will print out: 75 | 76 | ```js 77 | let phrase = "that's all folks"; 78 | console.log(phrase[phrase.length]); 79 | console.log(phrase[phrase.length - 1]); 80 | console.log(phrase[phrase.length - 2]); 81 | 82 | const i = 9; 83 | const char = phrase[i]; 84 | console.log(char); 85 | console.log(phrase.indexOf(char)); 86 | console.log(phrase.slice(2, 8)); 87 | 88 | console.log("abcdefg".slice(1, 3)); 89 | console.log("abcdefg".slice(2)); 90 | console.log("abcdefg".slice(4)); 91 | console.log("abcdefg".slice(2, -1)); 92 | console.log("abcdefg".slice(2, -2)); 93 | ``` 94 | 95 | Nice code. Run it. 96 | 97 | ## 3. A Tedious Task 98 | 99 | Inside of the `string-indexing-and-methods` folder create another file named `a-tedious-task.js`. 100 | Write the program so that it prints out the index where the substring `HEY` can be found in the text 101 | below. 102 | 103 | ```plaintext 104 | MIICWwIBAAKBgGEdLjFEFbegPZ2AwJWkalksXr7PzWL7wIc7pOFZxXwYPWtQxvANyceCwpkqbPLsfEx7nqxAris2hYOdeN1OTFqvTyNmVuzbUPcXShn6ZoDCB30voHkeu4F3cUw5RQEUDdLscSnv4HMxHam5qgl6vXoumVNHbjyKA5UtAnfjAgMBAAECgYAmjEyvpZTxRJvwjxDi1VaZevFI0Hd4WPH9PAGgqdnH84vGXnAGFj1WikqKYcqKMQW2kdjAsWwH9D9FfrkIcDDHdZ9XuGSGkFzWtOwajWMQl7qNV1hZ288gdpIQQMOTLDgauZY6pw1cV7h4v316qJB8knQGoBNpJCfTYQJBAKV1ctsJq0Zg4QumD2hyODepP3LfLeaQsERLqVAWeuOuTY5mK5gIwsSqvcSVfY7Ze1FWIsApNFRv67azKcJPwsCQQCNlyApZFJEVNY70Er7Uu5NL9t4CYJJC9uVVkoEHEY6d7sVslqa0vP2q0zXx9YedbMBvQjxXIbY0waXUy63FvoBGJAkB3OTJWUjVgzDY1Br5wu2Yu59NjKVKLWzCsu1gaCNBfhVDX7SyIyC9EYKRfUAoQxwsmPWPyQ9QVG4WKcPZJAkBRheAotPCBE2RLHHfvpiStnMhX0UXdVyaJp5tcZ6wYV61ohyBvCOkYhUxBJzeIGrVZcvLZSLeUzXoqRPpxQxAkEAkdCZXF0gHahpZgF5y0wWcqf9ECRT1E4Hv8bk3Mf0Exp2aW34JeI6I7Xqd1NV4I9H7prQ8m3y39lFwWO8PmQ 105 | ``` 106 | 107 | Fine. You can copy and paste, _this_ time. 108 | 109 | ## Walkthrough 110 | 111 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 112 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 113 | Watch it in 1.5x speed if you like. 114 | -------------------------------------------------------------------------------- /00_expressions_variables/solutions/A_executing_code_solution/age.js: -------------------------------------------------------------------------------- 1 | console.log(0); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/A_executing_code_solution/greeting.js: -------------------------------------------------------------------------------- 1 | console.log('hello world'); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/A_executing_code_solution/thirsty.js: -------------------------------------------------------------------------------- 1 | console.log('coffee'); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/A_executing_code_solution/whoami.js: -------------------------------------------------------------------------------- 1 | console.log('alvin'); 2 | console.log('z'); 3 | -------------------------------------------------------------------------------- /00_expressions_variables/solutions/B_expressions_solution/boolean-expressions.js: -------------------------------------------------------------------------------- 1 | console.log(false); // false 2 | console.log(!true); // false 3 | console.log(!false); // true 4 | console.log(!!true); // true 5 | 6 | console.log(false && false); // false 7 | console.log(false && true); // false 8 | console.log(true && false); // false 9 | console.log(true && true); // true 10 | 11 | console.log(false || false); // false 12 | console.log(false || true); // true 13 | console.log(true || false); // true 14 | console.log(true || true); // true 15 | 16 | console.log(!false || false); // true 17 | console.log(false || (true && true)); // true 18 | console.log(false || !(true && true)); // false 19 | console.log(!true && (true)); // false -------------------------------------------------------------------------------- /00_expressions_variables/solutions/B_expressions_solution/comparisons.js: -------------------------------------------------------------------------------- 1 | console.log(true === false); // false 2 | console.log(false === false); // true 3 | console.log(false !== true); // true 4 | console.log(!true === false); // true 5 | 6 | console.log(2 + 3 === 5); // true 7 | console.log(4 < 0); // false 8 | console.log(10 >= 10); // true 9 | console.log(10.3 >= 10); // true 10 | 11 | console.log(100 / 2 === 50); // true 12 | console.log(100 % 2 === 0); // true 13 | console.log(11 % 2 === 0); // false 14 | console.log(7.0 === 7); // true 15 | 16 | console.log(13 % 5 > 0); // true 17 | console.log("potato" === "potato"); // true 18 | console.log("Tomato" === "tomato"); // false 19 | console.log("42" === 42); // false 20 | console.log(5 > 3 && 1 === 0); // false -------------------------------------------------------------------------------- /00_expressions_variables/solutions/B_expressions_solution/number-expressions.js: -------------------------------------------------------------------------------- 1 | console.log(2 + 3); // 5 2 | console.log(10 - 15); // -5 3 | console.log(4 + 1 - 5); // 0 4 | console.log(4 * 3);// 12 5 | 6 | console.log(7 / 2); // 3.5 7 | console.log(4 + 2 * 3); // 10 8 | console.log((4 + 2) * 3); // 18 9 | console.log(5 % 2); // 1 10 | 11 | console.log(6 % 2); // 0 12 | console.log(7 % 2); // 1 13 | console.log(8 % 2); // 0 14 | console.log(19 % 8); // 3 15 | 16 | console.log(24 % 8); // 0 17 | console.log(7 % 4); // 3 18 | console.log(4 % 7); // 4 19 | console.log(5 + (10 % 5)); // 5 20 | console.log((5 + 10) % 5); // 0 -------------------------------------------------------------------------------- /00_expressions_variables/solutions/B_expressions_solution/string-expressions.js: -------------------------------------------------------------------------------- 1 | console.log("river" + "town"); 2 | console.log("cat" + "dog"); 3 | console.log("New" + " York"); 4 | console.log("runner's knee" + "!"); 5 | console.log("man" + "bear" + "pig"); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/C_variables_solution/four.js: -------------------------------------------------------------------------------- 1 | let movie = 'Fight Club'; 2 | 3 | let views = 4; 4 | 5 | console.log('I watched ' + movie + ' about ' + views + ' number of times'); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/C_variables_solution/one.js: -------------------------------------------------------------------------------- 1 | let word = "bye"; 2 | console.log(word + " felicia"); // 'bye felicia' 3 | console.log(word); // 'bye' 4 | 5 | let num = 10; 6 | num = num * 2; 7 | console.log(num); // 20 8 | 9 | let bottlesOfBeer = 99; 10 | let around = bottlesOfBeer - 1; 11 | console.log(around); // 98 12 | console.log(bottlesOfBeer); // 99 -------------------------------------------------------------------------------- /00_expressions_variables/solutions/C_variables_solution/three.js: -------------------------------------------------------------------------------- 1 | let qty = 15 % 4; 2 | console.log(qty); // 3 3 | 4 | let num = 38 + 3; 5 | num++; 6 | console.log(num + " is a great number"); // '42 is a great number' 7 | 8 | let isNumEven = num % 2 === 0; 9 | console.log(num + " is even? " + isNumEven); // '42 is even? true' 10 | 11 | let isQtyEven = qty % 2 === 0; 12 | console.log(qty + " is even? " + isQtyEven); // '3 is even? false' -------------------------------------------------------------------------------- /00_expressions_variables/solutions/C_variables_solution/two.js: -------------------------------------------------------------------------------- 1 | let apple; 2 | console.log(apple); // undefined 3 | 4 | apple = 5; 5 | console.log(apple); // 5 6 | 7 | apple + 1; 8 | console.log(apple); // 5 9 | 10 | apple += 1; 11 | console.log(apple); // 6 12 | 13 | let banana = apple; 14 | console.log(banana); // 6 15 | 16 | banana = banana / 2; 17 | console.log(banana); // 3 18 | 19 | console.log(apple); // 6 -------------------------------------------------------------------------------- /00_expressions_variables/solutions/C_variables_solution/zero.js: -------------------------------------------------------------------------------- 1 | let location = "Brooklyn, " + "NY"; 2 | console.log(location); // 'Brooklyn, NY' 3 | 4 | let quantity = 4 * 5 + 1; 5 | console.log(quantity); // 21 -------------------------------------------------------------------------------- /00_expressions_variables/solutions/D_string_indexing_and_methods_solution/a-tedious-task.js: -------------------------------------------------------------------------------- 1 | let str = 'MIICWwIBAAKBgGEdLjFEFbegPZ2AwJWkalksXr7PzWL7wIc7pOFZxXwYPWtQxvANyceCwpkqbPLsfEx7nqxAris2hYOdeN1OTFqvTyNmVuzbUPcXShn6ZoDCB30voHkeu4F3cUw5RQEUDdLscSnv4HMxHam5qgl6vXoumVNHbjyKA5UtAnfjAgMBAAECgYAmjEyvpZTxRJvwjxDi1VaZevFI0Hd4WPH9PAGgqdnH84vGXnAGFj1WikqKYcqKMQW2kdjAsWwH9D9FfrkIcDDHdZ9XuGSGkFzWtOwajWMQl7qNV1hZ288gdpIQQMOTLDgauZY6pw1cV7h4v316qJB8knQGoBNpJCfTYQJBAKV1ctsJq0Zg4QumD2hyODepP3LfLeaQsERLqVAWeuOuTY5mK5gIwsSqvcSVfY7Ze1FWIsApNFRv67azKcJPwsCQQCNlyApZFJEVNY70Er7Uu5NL9t4CYJJC9uVVkoEHEY6d7sVslqa0vP2q0zXx9YedbMBvQjxXIbY0waXUy63FvoBGJAkB3OTJWUjVgzDY1Br5wu2Yu59NjKVKLWzCsu1gaCNBfhVDX7SyIyC9EYKRfUAoQxwsmPWPyQ9QVG4WKcPZJAkBRheAotPCBE2RLHHfvpiStnMhX0UXdVyaJp5tcZ6wYV61ohyBvCOkYhUxBJzeIGrVZcvLZSLeUzXoqRPpxQxAkEAkdCZXF0gHahpZgF5y0wWcqf9ECRT1E4Hv8bk3Mf0Exp2aW34JeI6I7Xqd1NV4I9H7prQ8m3y39lFwWO8PmQ'; 2 | 3 | console.log(str.indexOf('HEY')); -------------------------------------------------------------------------------- /00_expressions_variables/solutions/D_string_indexing_and_methods_solution/dare-to-decipher.js: -------------------------------------------------------------------------------- 1 | let phrase = "that's all folks"; 2 | console.log(phrase[phrase.length]); // undefined 3 | console.log(phrase[phrase.length - 1]); // s 4 | console.log(phrase[phrase.length - 2]); // k 5 | 6 | const i = 9; 7 | const char = phrase[i]; 8 | console.log(char); // l 9 | console.log(phrase.indexOf(char)); // 8 10 | console.log(phrase.slice(2, 8)); // at's a 11 | 12 | console.log("abcdefg".slice(1, 3)); // bc 13 | console.log("abcdefg".slice(2)); // cdefg 14 | console.log("abcdefg".slice(4)); // efg 15 | 16 | 17 | console.log("abcdefg".slice(2, -1)); // cdef 18 | console.log("abcdefg".slice(2, -2)); // cde -------------------------------------------------------------------------------- /00_expressions_variables/solutions/D_string_indexing_and_methods_solution/proper-patterns.js: -------------------------------------------------------------------------------- 1 | let word = "suspension bridge"; 2 | console.log(word[4]); // e 3 | console.log(word.length > 5 && word[0] === "d"); // false 4 | console.log(word.length > 5 && word[0] === "s"); // true 5 | console.log(word.indexOf("o") > -1); // true 6 | console.log(word.indexOf("z") > -1); // false 7 | 8 | let str = "foggy"; 9 | console.log(str[2 + 1]); // g 10 | console.log(str[str.length - 1]); // y 11 | str = " day"; 12 | console.log(str); // ' day' 13 | console.log(str.length); // 4 14 | console.log(str.indexOf("ogg")); // -1 -------------------------------------------------------------------------------- /00_expressions_variables/solutions/D_string_indexing_and_methods_solution/small-string-snippets.js: -------------------------------------------------------------------------------- 1 | console.log("promenade"[3]); // m 2 | console.log("tiger"[1]); // i 3 | console.log("wheel".length); // 5 4 | 5 | console.log("wheel".length - 1); // 4 6 | console.log("noMAD".toUpperCase()); // NOMAD 7 | console.log("hey programmers"[2] === "y"); // true 8 | 9 | console.log("volleyball".length > 20); // false 10 | console.log("treasure".indexOf("r")); // 1 11 | console.log("treasure".indexOf("e")); // 2 12 | console.log("web"[5]); // undefined 13 | console.log("red".indexOf("x")); // -1 14 | console.log("red".indexOf("R")); // -1 -------------------------------------------------------------------------------- /01_conditionals/exercises/A_conditionals_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Strength and Conditioning 2 | 3 | A _conditional_ is a construct that allows programmers to execute different code dependent on the 4 | value of a boolean expression. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture](https://youtu.be/ous6eeG2BaY) 9 | 10 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 11 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 12 | 13 | + [Walkthrough Part 1](https://youtu.be/o50tHXYQzFE) 14 | + [Walkthrough Part 2](https://youtu.be/Ij7V0JfxeUs) 15 | 16 | _If_ you want to learn how to code, _then_ code. 17 | 18 | -AZ 19 | 20 | ## 0. If you can do this, then move on. 21 | 22 | Create a new folder named `conditional_exercise`. Inside, create a file named `zero.js`. Predict 23 | what each of the code snippets below will print out. After you make a prediction for a snippet, add 24 | the snippet into `zero.js`. Don't copy and paste. Run the code to verify your prediction for that 25 | snippet, then move on to the next snippet. 26 | 27 | ```js 28 | // snippet 0-1 29 | if (true) { 30 | console.log("foo"); 31 | } 32 | 33 | if (false) { 34 | console.log("bar"); 35 | } 36 | ``` 37 | 38 | ```js 39 | // snippet 0-2 40 | if (false || false) { 41 | console.log("boop"); 42 | } 43 | 44 | if (true || false) { 45 | console.log("beep"); 46 | } 47 | ``` 48 | 49 | ```js 50 | // snippet 0-3 51 | let num = 40; 52 | 53 | if (num > 0) { 54 | console.log("zip"); 55 | } 56 | 57 | if (num % 2 === 0) { 58 | console.log("zoop"); 59 | } 60 | ``` 61 | 62 | ```js 63 | // snippet 0-4 64 | let word = "jeep"; 65 | 66 | if (word[0] === "d") { 67 | console.log("yer"); 68 | } else { 69 | console.log("nah"); 70 | } 71 | ``` 72 | 73 | ```js 74 | // snippet 0-5 75 | let sentence = "roger that"; 76 | 77 | if (sentence[sentence.length - 1] === "t") { 78 | console.log("ends in t"); 79 | } else { 80 | console.log("does not end in t"); 81 | } 82 | 83 | if (sentence.length <= 4) { 84 | console.log("short"); 85 | } else { 86 | console.log("long"); 87 | } 88 | ``` 89 | 90 | ## 1. Conditions Do Apply. 91 | 92 | Create a new file named `one.js` inside of the `conditional_exercise` folder. Predict what each of 93 | the snippets below will print out. Type each snippet into the file and run it to test your 94 | prediction. Don't copy and paste. 95 | 96 | ```js 97 | // snippet 1-0 98 | let qty = 38; 99 | 100 | if (qty > 30 && qty % 5 === 4) { 101 | console.log("swish"); 102 | } else { 103 | console.log("swoosh"); 104 | } 105 | 106 | if (qty > 0) { 107 | console.log("pos"); 108 | } 109 | ``` 110 | 111 | ```js 112 | // snippet 1-1 113 | let a = "celery"; 114 | let b = "SQUASH"; 115 | 116 | if (a === a.toUpperCase()) { 117 | console.log("alpha"); 118 | } 119 | 120 | if (b === b.toUpperCase()) { 121 | console.log("beta"); 122 | } 123 | ``` 124 | 125 | ```js 126 | // snippet 1-2 127 | let number = 9; 128 | 129 | if (number > 4) { 130 | console.log("ding"); 131 | } else if (number % 3 === 0) { 132 | console.log("dong"); 133 | } 134 | ``` 135 | 136 | ```js 137 | // snippet 1-3 138 | let z = 12; 139 | 140 | if (z > 10) { 141 | console.log("vroom"); 142 | } 143 | 144 | if (z % 3 === 0) { 145 | console.log("skrrt"); 146 | } 147 | ``` 148 | 149 | ## 2. Iffy. 150 | 151 | Create a new file named `two.js` inside of the `conditional_exercise` folder. Predict what each of 152 | the snippets below will print out. Type each snippet into the file and run it to test your 153 | prediction. Don't copy and paste. 154 | 155 | ```js 156 | // snippet 2-1 157 | let nonsense = "blog trust fund tattooed williamsburg poke roof party"; 158 | let hasOk = nonsense.indexOf("ok") > -1; 159 | 160 | if (hasOk) { 161 | console.log("yeet"); 162 | } else if (nonsense.length > 10) { 163 | console.log("yo"); 164 | } else { 165 | console.log("no"); 166 | } 167 | 168 | let hasZoo = nonsense.indexOf("zoo") > -1; 169 | let hasFun = nonsense.indexOf("fun") > -1; 170 | 171 | if (hasZoo && hasOk) { 172 | console.log("cool"); 173 | } else if (hasOk) { 174 | console.log("rad"); 175 | } else if (hasFun) { 176 | console.log("dope"); 177 | } else { 178 | console.log("nope"); 179 | } 180 | ``` 181 | 182 | ```js 183 | // snippet 2-2 184 | 185 | let q = 25; 186 | if (q % 3 === 0 && q % 5 === 0) { 187 | console.log("both"); 188 | } else if (q % 3 === 0 || q % 5 == 0) { 189 | console.log("either"); 190 | } else { 191 | console.log("neither"); 192 | } 193 | 194 | let r = 9; 195 | if (r % 3 === 0 && r % 5 === 0) { 196 | console.log("both"); 197 | } else if (r % 3 === 0 || r % 5 == 0) { 198 | console.log("either"); 199 | } else { 200 | console.log("neither"); 201 | } 202 | 203 | let s = 15; 204 | if (s % 3 === 0 && s % 5 === 0) { 205 | console.log("both"); 206 | } else if (s % 3 === 0 || s % 5 == 0) { 207 | console.log("either"); 208 | } else { 209 | console.log("neither"); 210 | } 211 | ``` 212 | 213 | ## 3. Conditional Love. 214 | 215 | Create a new file named `three.js` inside of the `conditional_exercise` folder. The program should 216 | print out `FOUND` if the substring `coDe` exists in the text below. If the `coDe` does not exist in 217 | the text, then the program should print `NOT FOUND`. 218 | 219 | ```plaintext 220 | AIICXAIBAAKBgQCuw0YyucjI9bf7yRhIkyg4Ru6kYU7O6fIn2JoFDzCZNkzDdsuXFGh6BXNvbu8uZUT289ERzYP1QjryMEKWzcbtsioyQApL7AgOZyFc3JJ7wvupHhIj2sqxJNtpAh7HQFG08rYh2Pb3HwOm83rbTomM6LnnjooGcoDeuWkuPqXsRlwIDAQABAoGAeQkbPBR5n9y2QLaEjcDGv7dVpFiMGHMaZZVDX34rZPy1EkZNZqlQU0jopLVvLyLESMh9A7gKhqoyMAbgZPpdb0CvniTJPpKYk24mLBeym8rBMW3XBmKk1xIOcJPGXMxeJW61jxPg6doah0aCIjf8n0Z8t8B81kLFojpECQQDzlCp0Nzka3AVZVPdBuuPg0fzeV8ugpiPEp6wQLjIMDFqYtGoAOEy4JpkmkK7zwsQsHZ8jbOtqIFdRLPrvAkEAruU321Ie1CnYCHX4Q79vLcDeWOUpdzEHp2uzDIfzP1gv7RIktGgSZWKhrnNWdeH4Y0CFb9lu6TQYJwNJvug2QJBANX0m3Uds9P9pLbQlI9WWmAtYqIZrsBElcAjOgAik0uOfawholNiw5B3ADvIYqPkLW4dGk1dO6zxW8ZF83MdMCQHGfhxLuFgsOBSfF7Bj4UX6T9FGhUGSXiqUsd06E2mMRLAjWUUUw82DLwucxMrSsV4z1aN57asC8YuQ9FkCQGirlVCt4ccXeGLCHcCsI8AYQJFRzbMs381M16jEpnaKUGrtdbMW018gWJ0EoD4tS2YZpr1hEbtiOkPJaaTKQ 221 | ``` 222 | 223 | You may copy and paste. What a time to be alive. 224 | 225 | ## Walkthrough 226 | 227 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 228 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 229 | Watch it in 1.5x speed if you like. 230 | -------------------------------------------------------------------------------- /01_conditionals/solutions/A_conditionals_solution/one.js: -------------------------------------------------------------------------------- 1 | // snippet 1-0 2 | let qty = 38; 3 | 4 | if (qty > 30 && qty % 5 === 4) { 5 | console.log("swish"); 6 | } else { 7 | console.log("swoosh"); // prints 8 | } 9 | 10 | if (qty > 0) { 11 | console.log("pos"); // prints 12 | } 13 | 14 | // snippet 1-1 15 | let a = "celery"; 16 | let b = "SQUASH"; 17 | 18 | if (a === a.toUpperCase()) { 19 | console.log("alpha"); 20 | } 21 | 22 | if (b === b.toUpperCase()) { 23 | console.log("beta"); // prints 24 | } 25 | 26 | 27 | // snippet 1-2 28 | let number = 9; 29 | 30 | if (number > 4) { 31 | console.log("ding"); // prints 32 | } else if (number % 3 === 0) { 33 | console.log("dong"); 34 | } 35 | 36 | 37 | 38 | // snippet 1-3 39 | let z = 12; 40 | 41 | if (z > 10) { 42 | console.log("vroom"); // prints 43 | } 44 | 45 | if (z % 3 === 0) { 46 | console.log("skrrt"); // prints 47 | } -------------------------------------------------------------------------------- /01_conditionals/solutions/A_conditionals_solution/three.js: -------------------------------------------------------------------------------- 1 | let longStr = "AIICXAIBAAKBgQCuw0YyucjI9bf7yRhIkyg4Ru6kYU7O6fIn2JoFDzCZNkzDdsuXFGh6BXNvbu8uZUT289ERzYP1QjryMEKWzcbtsioyQApL7AgOZyFc3JJ7wvupHhIj2sqxJNtpAh7HQFG08rYh2Pb3HwOm83rbTomM6LnnjooGcoDeuWkuPqXsRlwIDAQABAoGAeQkbPBR5n9y2QLaEjcDGv7dVpFiMGHMaZZVDX34rZPy1EkZNZqlQU0jopLVvLyLESMh9A7gKhqoyMAbgZPpdb0CvniTJPpKYk24mLBeym8rBMW3XBmKk1xIOcJPGXMxeJW61jxPg6doah0aCIjf8n0Z8t8B81kLFojpECQQDzlCp0Nzka3AVZVPdBuuPg0fzeV8ugpiPEp6wQLjIMDFqYtGoAOEy4JpkmkK7zwsQsHZ8jbOtqIFdRLPrvAkEAruU321Ie1CnYCHX4Q79vLcDeWOUpdzEHp2uzDIfzP1gv7RIktGgSZWKhrnNWdeH4Y0CFb9lu6TQYJwNJvug2QJBANX0m3Uds9P9pLbQlI9WWmAtYqIZrsBElcAjOgAik0uOfawholNiw5B3ADvIYqPkLW4dGk1dO6zxW8ZF83MdMCQHGfhxLuFgsOBSfF7Bj4UX6T9FGhUGSXiqUsd06E2mMRLAjWUUUw82DLwucxMrSsV4z1aN57asC8YuQ9FkCQGirlVCt4ccXeGLCHcCsI8AYQJFRzbMs381M16jEpnaKUGrtdbMW018gWJ0EoD4tS2YZpr1hEbtiOkPJaaTKQ"; 2 | 3 | if (longStr.indexOf('coDe') > -1) { 4 | console.log('FOUND'); 5 | } else { 6 | console.log('NOT FOUND'); 7 | } -------------------------------------------------------------------------------- /01_conditionals/solutions/A_conditionals_solution/two.js: -------------------------------------------------------------------------------- 1 | // snippet 2-1 2 | let nonsense = "blog trust fund tattooed williamsburg poke roof party"; 3 | let hasOk = nonsense.indexOf("ok") > -1; 4 | 5 | if (hasOk) { 6 | console.log("yeet"); // prints 7 | } else if (nonsense.length > 10) { 8 | console.log("yo"); 9 | } else { 10 | console.log("no"); 11 | } 12 | 13 | let hasZoo = nonsense.indexOf("zoo") > -1; 14 | let hasFun = nonsense.indexOf("fun") > -1; 15 | 16 | if (hasZoo && hasOk) { 17 | console.log("cool"); 18 | } else if (hasOk) { 19 | console.log("rad"); // prints 20 | } else if (hasFun) { 21 | console.log("dope"); 22 | } else { 23 | console.log("nope"); 24 | } 25 | 26 | // snippet 2-2 27 | let q = 25; 28 | if (q % 3 === 0 && q % 5 === 0) { 29 | console.log("both"); 30 | } else if (q % 3 === 0 || q % 5 == 0) { 31 | console.log("either"); // prints 32 | } else { 33 | console.log("neither"); 34 | } 35 | 36 | let r = 9; 37 | if (r % 3 === 0 && r % 5 === 0) { 38 | console.log("both"); 39 | } else if (r % 3 === 0 || r % 5 == 0) { 40 | console.log("either"); // prints 41 | } else { 42 | console.log("neither"); 43 | } 44 | 45 | let s = 15; 46 | if (s % 3 === 0 && s % 5 === 0) { 47 | console.log("both"); // prints 48 | } else if (s % 3 === 0 || s % 5 == 0) { 49 | console.log("either"); 50 | } else { 51 | console.log("neither"); 52 | } -------------------------------------------------------------------------------- /01_conditionals/solutions/A_conditionals_solution/zero.js: -------------------------------------------------------------------------------- 1 | // snippet 0-1 2 | if (true) { 3 | console.log("foo"); // prints 4 | } 5 | 6 | if (false) { 7 | console.log("bar"); 8 | } 9 | 10 | 11 | // snippet 0-2 12 | if (false || false) { 13 | console.log("boop"); 14 | } 15 | 16 | if (true || false) { 17 | console.log("beep"); // prints 18 | } 19 | 20 | 21 | // snippet 0-3 22 | let num = 40; 23 | 24 | if (num > 0) { 25 | console.log("zip"); // prints 26 | } 27 | 28 | if (num % 2 === 0) { 29 | console.log("zoop"); // prints 30 | } 31 | 32 | 33 | // snippet 0-4 34 | let word = "jeep"; 35 | 36 | if (word[0] === "d") { 37 | console.log("yer"); 38 | } else { 39 | console.log("nah"); // prints 40 | } 41 | 42 | 43 | // snippet 0-5 44 | let sentence = "roger that"; 45 | 46 | if (sentence[sentence.length - 1] === "t") { 47 | console.log("ends in t"); // prints 48 | } else { 49 | console.log("does not end in t"); 50 | } 51 | 52 | if (sentence.length <= 4) { 53 | console.log("short"); 54 | } else { 55 | console.log("long"); // prints 56 | } -------------------------------------------------------------------------------- /02_functions/exercises/A_functions_exercise/README.md: -------------------------------------------------------------------------------- 1 | # What's your function? 2 | 3 | _Functions_ are constructs that allow programmers to group multiple statements together into a 4 | single logical unit. Functions may take _arguments_ as input and return some value as output. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture](https://youtu.be/g7fBqWTdeMw) 9 | 10 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 11 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 12 | 13 | This is where we bring well defined structure to our code. If so far we've only learned the isolated 14 | "words", "phrases", and "sentences" of programming, we now turn our attention to the "paragraphs". 15 | 16 | + [Walkthrough Part 1](https://youtu.be/hS-p8ICd7Ek) 17 | + [Walkthrough Part 2](https://youtu.be/pE9JYGobDQg) 18 | 19 | -AZ 20 | 21 | ## 0. Practice how to read. 22 | 23 | Before you write functions on your own, let's warm up by interpretting a few. 24 | 25 | Create a new folder named `A_functions_exercise`. Predict what would print when executing each of 26 | the snippets below. Test your prediction by typing the snippet into their own files and executing 27 | the program. Don't copy and paste. 28 | 29 | Write the below code into a file named `snippet-0-1.js`. Make a prediction for what it will print. 30 | Run it. 31 | 32 | ```js 33 | // snippet 0-1 34 | let greet = function () { 35 | console.log("hey"); 36 | console.log("programmers"); 37 | }; 38 | 39 | let whistle = function () { 40 | console.log("doot"); 41 | }; 42 | 43 | console.log("first"); 44 | console.log("second"); 45 | greet(); 46 | console.log("third"); 47 | console.log("fourth"); 48 | whistle(); 49 | ``` 50 | 51 | Write the below code into a file named `snippet-0-2.js`. Make a prediction for what it will print. 52 | Run it. 53 | 54 | ```js 55 | // snippet 0-2 56 | let howMany = function () { 57 | return 42; 58 | }; 59 | 60 | console.log(howMany); 61 | console.log(howMany()); 62 | 63 | const theAnswer = howMany(); 64 | console.log(theAnswer); 65 | 66 | let howMuch = function () { 67 | 5; 68 | }; 69 | console.log(howMuch()); 70 | ``` 71 | 72 | Write the below code into a file named `snippet-0-3.js`. Make a prediction for what it will print. 73 | Run it. 74 | 75 | ```js 76 | // snippet 0-3 77 | let average = function (num1, num2) { 78 | console.log("calculating..."); 79 | return (num1 + num2) / 2; 80 | }; 81 | 82 | console.log(average(5, 10)); 83 | console.log(average(20, 26)); 84 | console.log(average(50, 100) + 2); 85 | 86 | let a = 21 + 3; 87 | let b = 20; 88 | let n = average(a, b); 89 | console.log(average(n, 18)); 90 | ``` 91 | 92 | Write the below code into a file named `snippet-0-4.js`. Make a prediction for what it will print. 93 | Run it. 94 | 95 | ```js 96 | // snippet 0-4 97 | let exclaim = function (str) { 98 | let capitalized = str.toUpperCase(); 99 | return capitalized + "!!"; 100 | }; 101 | 102 | let result = exclaim("potato"); 103 | console.log(result); 104 | console.log(result.length); 105 | console.log(result[0]); 106 | console.log(result[result.length - 1]); 107 | ``` 108 | 109 | ## 1. Practice how to write. 110 | 111 | In this section of the exercise you'll be prompted to write a function to accomplish a specific 112 | task. Read the description and look at the example output to figure out what should be implemented 113 | within the function. Write each function to a separate file in the `A_functions_exercise` folder and 114 | test your work by running the file. 115 | 116 | ### isDivBy4 117 | 118 | Create an `is-div-by-4.js` file and solve the function below: 119 | 120 | ```js 121 | // Write a function `isDivBy4` that accepts a number as an argument. The function should return a 122 | // boolean indicating whether or not the number is divisible by 4. 123 | 124 | console.log(isDivBy4(8)); // true 125 | console.log(isDivBy4(12)); // true 126 | console.log(isDivBy4(24)); // true 127 | console.log(isDivBy4(9)); // false 128 | console.log(isDivBy4(10)); // false 129 | ``` 130 | 131 | ### keepItQuiet 132 | 133 | Create a `keep-it-quiet.js` file and solve the function below: 134 | 135 | ```js 136 | // Write a function `keepItQuiet` that accepts a string as an argument. The function should return the 137 | // lowercase version of the string with 3 periods added to the end of it. 138 | 139 | console.log(keepItQuiet("HOORAY")); // 'hooray...' 140 | console.log(keepItQuiet("Doggo")); // 'doggo...' 141 | console.log(keepItQuiet("WHAT?!?!")); // 'what?!?!...' 142 | ``` 143 | 144 | ### isLong 145 | 146 | Create an `is-long.js` file and solve the function below: 147 | 148 | 149 | ```js 150 | // Write a function `isLong` that accepts a string as an argument. The function should return a boolean 151 | // indicating whether or not the string is longer than 5 characters 152 | 153 | console.log(isLong("pie")); // false 154 | console.log(isLong("kite")); // false 155 | console.log(isLong("kitty")); // false 156 | console.log(isLong("telescope")); // true 157 | console.log(isLong("thermometer")); // true 158 | console.log(isLong("restaurant")); // true 159 | ``` 160 | 161 | ### half 162 | 163 | Create a `half.js` file and solve the function below: 164 | 165 | 166 | ```js 167 | // Write a function `half` that accepts a number as an argument. The function should return half of the 168 | // number. 169 | 170 | console.log(half(8)); // 4 171 | console.log(half(15)); // 7.5 172 | console.log(half(90)); // 45 173 | ``` 174 | 175 | ### endsWithT 176 | 177 | Create an `endsWithT.js` file and solve the function below: 178 | 179 | ```js 180 | // Write a function `endsWithT` that accepts a string as an argument. The function should return a 181 | // boolean indicating whether or not the string ends with the character 't'. 182 | 183 | console.log(endsWithT("smart")); // true 184 | console.log(endsWithT("racket")); // true 185 | console.log(endsWithT("taco")); // false 186 | console.log(endsWithT("boomerang")); // false 187 | ``` 188 | 189 | ### average 190 | 191 | Create an `average.js` file and solve the function below: 192 | 193 | 194 | ```js 195 | // Write a function `average` that accepts three numbers as arguments. The function should return the 196 | // average of the three numbers. 197 | 198 | console.log(average(3, 10, 8)); // 7 199 | console.log(average(10, 5, 12)); // 9 200 | console.log(average(6, 20, 40)); // 22 201 | ``` 202 | 203 | ## Walkthrough 204 | 205 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 206 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. Watch it 207 | in 1.5x speed if you like. 208 | -------------------------------------------------------------------------------- /02_functions/exercises/B_functions_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Functional Fitness 2 | 3 | _Functions_ group together statements deliberately with a purpose in mind. They should do one thing 4 | and do it well. 5 | 6 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 7 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 8 | 9 | + [Walkthrough](https://youtu.be/YQjR-TSWE-U) 10 | 11 | Programmers write functions. You are a programmer. You write functions. 12 | 13 | -AZ 14 | 15 | ## Instructions 16 | 17 | Create a new folder named `B_functions_exercise`, this is where you'll save all of your files. Solve 18 | each of the prompts below. Create a new file for each prompt and execute your code to see if it 19 | matches the example output. 20 | 21 | ### startsWithR 22 | 23 | Create a `starts-with-r.js` file and solve the function below: 24 | 25 | ```js 26 | // Write a function `startsWithR` that accepts a string as an argument and returns a boolean indicating 27 | // whether or not the string starts with 'r' or 'R'. 28 | 29 | console.log(startsWithR("roger that")); // true 30 | console.log(startsWithR("Row, row, row your boat")); // true 31 | console.log(startsWithR("slip")); // false 32 | console.log(startsWithR("Taxicab")); // false 33 | ``` 34 | 35 | ### parity 36 | 37 | Create a `parity.js` file and solve the function below: 38 | 39 | ```js 40 | // Write a function `parity` that accepts a number as an argument. The function should return the 41 | // string 'even' if the number is even. It should return the string 'odd' if the number is odd. 42 | 43 | console.log(parity(5)); // 'odd' 44 | console.log(parity(7)); // 'odd' 45 | console.log(parity(13)); // 'odd' 46 | console.log(parity(32)); // 'even' 47 | console.log(parity(10)); // 'even' 48 | console.log(parity(602348)); // 'even' 49 | ``` 50 | 51 | ### longer 52 | 53 | Create a `longer.js` file and solve the function below: 54 | 55 | ```js 56 | // Write a function `longer` that accepts two strings as arguments. The function should return the 57 | // string that is longer. If the strings have the same length, then return the first string. 58 | 59 | console.log(longer("drum", "piranha")); // 'piranha' 60 | console.log(longer("basket", "fork")); // 'basket' 61 | console.log(longer("flannel", "sustainable")); // 'sustainable' 62 | console.log(longer("disrupt", "ability")); // 'disrupt' 63 | console.log(longer("bird", "shoe")); // 'bird' 64 | ``` 65 | 66 | ### oneOrNone 67 | 68 | Create a `one-or-none.js` file and solve the function below: 69 | 70 | ```js 71 | // Write a function `oneOrNone` that accepts two booleans as arguments. The function should return true 72 | // if exactly one of the arguments is true. If BOTH arguments are true, then it should return false. 73 | 74 | console.log(oneOrNone(false, false)); // false 75 | console.log(oneOrNone(true, false)); // true 76 | console.log(oneOrNone(false, true)); // true 77 | console.log(oneOrNone(true, true)); // false 78 | ``` 79 | 80 | ### endsInLy 81 | 82 | Create a `ends-in-ly.js` file and solve the function below: 83 | 84 | ```js 85 | // Write a function `endsInLy` that accepts a string as an argument and returns a boolean indicating 86 | // whether or not the string ends in the substring 'ly'. 87 | 88 | console.log(endsInLy("pretty")); // false 89 | console.log(endsInLy("instant")); // false 90 | console.log(endsInLy("analytic")); // false 91 | console.log(endsInLy("timidly")); // true 92 | console.log(endsInLy("fly")); // true 93 | console.log(endsInLy("gallantly")); // true 94 | ``` 95 | 96 | ### funnySound 97 | 98 | Create a `funny-sound.js` file and solve the function below: 99 | 100 | ```js 101 | // Write a function `funnySound` that accepts two strings as arguments. The function should return a 102 | // new string containing the first three characters of both strings concatenated together. 103 | 104 | // You can assume that the arguments are both at least three characters long. 105 | 106 | console.log(funnySound("tiger", "spoon")); // 'tigspo' 107 | console.log(funnySound("computer", "phone")); // 'compho' 108 | console.log(funnySound("skate", "bottle")); // 'skabot' 109 | console.log(funnySound("frog", "ashtray")); // 'froash' 110 | ``` 111 | 112 | ### stringSize 113 | 114 | Create a `string-size.js` file and solve the function below: 115 | 116 | ```js 117 | // Write a function `stringSize` that accepts a string as an argument. The function should return the 118 | // string 'small' if the argument is shorter than 5 characters, 'medium' if it is exactly 5 characters, and 119 | // 'large' if it is longer than 5 characters. 120 | 121 | console.log(stringSize("cat")); // 'small' 122 | console.log(stringSize("bell")); // 'small' 123 | console.log(stringSize("ready")); // 'medium' 124 | console.log(stringSize("shirt")); // 'medium' 125 | console.log(stringSize("shallow")); // 'large' 126 | console.log(stringSize("intelligence")); // 'large' 127 | ``` 128 | 129 | ### wackyWord 130 | 131 | Create a `wacky-word.js` file and solve the function below: 132 | 133 | ```js 134 | // Write a function `wackyWord` that accepts two strings as arguments. The function should return a new 135 | // string containing the first three characters of the first string concatenated with the last two 136 | // character of the second string. 137 | 138 | // You can assume that the first argument has a length of at least three and the second argument has a 139 | // length of at least two. 140 | 141 | console.log(wackyWord("very", "kindly")); // 'verly' 142 | console.log(wackyWord("forever", "sick")); // 'forck' 143 | console.log(wackyWord("cellar", "door")); // 'celor' 144 | console.log(wackyWord("bagel", "sweep")); // 'bagep' 145 | ``` 146 | 147 | ## Walkthrough 148 | 149 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 150 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 151 | Watch it in 1.5x speed if you like. 152 | -------------------------------------------------------------------------------- /02_functions/exercises/C_functions_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Major Malfunction 2 | 3 | More practice! 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | + [Walkthrough](https://youtu.be/KCv1ovMB4DU) 9 | 10 | I think the reason why coding has a reputation for being nonintuitive is because beginners don't 11 | spend enough time on the basics. They miss a step early, but instead of filling that gap of 12 | knowledge, they preemptively move on. They'll stumble through more advanced topics until that debt 13 | cannot be ignored. That is why I think it's important to exercise a concept extensively. Write code. 14 | Run it. 15 | 16 | -AZ 17 | 18 | ## Instructions 19 | 20 | Create a new folder named `C_functions_exercise`, this is where you'll save all of your files. Solve 21 | each of the prompts below. Create a new file for each prompt and execute your code to see if it 22 | matches the example output. 23 | 24 | ### divisible 25 | 26 | Create a `divisible.js` file and solve the function below: 27 | 28 | ```js 29 | // Write a function `divisible(num1, num2)` that accepts two numbers as arguments. The function should 30 | // return a boolean indicating whether or not `num1` is divisible by `num2`. 31 | 32 | console.log(divisible(12, 3)); // true 33 | console.log(divisible(12, 5)); // false 34 | console.log(divisible(60, 4)); // true 35 | console.log(divisible(60, 11)); // false 36 | console.log(divisible(21, 7)); // true 37 | console.log(divisible(21, 6)); // false 38 | ``` 39 | 40 | ### caseChanger 41 | 42 | Create a `case-changer.js` file and solve the function below: 43 | 44 | ```js 45 | // Write a function `caseChange` that accepts a string and a boolean as arguments. The function should 46 | // return the uppercase version of the string if the boolean is true. The function should return the 47 | // lowercase version of the string if the boolean is false. 48 | 49 | console.log(caseChange("Super", true)); // 'SUPER' 50 | console.log(caseChange("Super", false)); // 'super' 51 | console.log(caseChange("tAmBourine", true)); // 'TAMBOURINE' 52 | console.log(caseChange("tAmBourine", false)); // 'tambourine' 53 | ``` 54 | 55 | ### inRange 56 | 57 | Create a `in-range.js` file and solve the function below: 58 | 59 | ```js 60 | // Write a function `inRange(min, max, n)` that accepts three numbers as arguments. The function should 61 | // return a boolean indicating if `n` is between `min` and `max` inclusive. 62 | 63 | console.log(inRange(5, 13, 8)); // true 64 | console.log(inRange(5, 13, 29)); // false 65 | console.log(inRange(100, 125, 100)); // true 66 | console.log(inRange(100, 125, 99)); // false 67 | console.log(inRange(40, 45, 44)); // true 68 | console.log(inRange(40, 45, 45)); // true 69 | console.log(inRange(40, 45, 46)); // false 70 | ``` 71 | 72 | ### averageOfFour 73 | 74 | Create an `average-of-four.js` file and solve the function below: 75 | 76 | ```js 77 | // Write a function `averageOfFour(num1, num2, num3, num4)` that accepts four numbers as arguments. The 78 | // function should return the average of all four numbers. 79 | 80 | console.log(averageOfFour(10, 4, 12, 3)); // 7.25 81 | console.log(averageOfFour(-20, 50, 4, 21)); // 13.75 82 | console.log(averageOfFour(10, 4, 12, 3)); // 7.25 83 | console.log(averageOfFour(5, 5, 3, 7)); // 5 84 | ``` 85 | 86 | ### numberChange 87 | 88 | Create a `number-change.js` file and solve the function below: 89 | 90 | ```js 91 | // Write a function `numberChange` that accepts a number as an argument. The function should return 92 | // half the number if it is even. The function should return double the number if it is odd. 93 | 94 | console.log(numberChange(6)); // 3 95 | console.log(numberChange(7)); // 14 96 | console.log(numberChange(16)); // 8 97 | console.log(numberChange(21)); // 42 98 | ``` 99 | 100 | ### larger 101 | 102 | Create a `larger.js` file and solve the function below: 103 | 104 | ```js 105 | // Write a function `larger` that accepts two numbers as arguments. The function should return the 106 | // larger number. 107 | 108 | console.log(larger(256, 400)); // 400 109 | console.log(larger(31, 4)); // 31 110 | console.log(larger(-6, 7)); // 7 111 | console.log(larger(11.3, 11.2)); // 11.3 112 | console.log(larger(-10, -3)); // -3 113 | ``` 114 | 115 | ### contains 116 | 117 | Create a `contains.js` file and solve the function below: 118 | 119 | ```js 120 | // Write a function `contains(str1, str2)` that accepts two strings as arguments. The function should 121 | // return a boolean indicating whether or not `str2` is contained within `str1`. The function should 122 | // ignore any differences in capitalization. 123 | 124 | console.log(contains("caterpillar", "pill")); // true 125 | console.log(contains("lion's share", "on")); // true 126 | console.log(contains("SORRY", "or")); // true 127 | console.log(contains("tangent", "gem")); // false 128 | console.log(contains("clock", "ok")); // false 129 | ``` 130 | 131 | 132 | ## Walkthrough 133 | 134 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 135 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 136 | Watch it in 1.5x speed if you like. 137 | -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/average.js: -------------------------------------------------------------------------------- 1 | // Write a function `average` that accepts three numbers as arguments. The function should return the 2 | // average of the three numbers. 3 | 4 | let average = function(num1, num2, num3) { 5 | return (num1 + num2 + num3) / 3; 6 | }; 7 | 8 | console.log(average(3, 10, 8)); // 7 9 | console.log(average(10, 5, 12)); // 9 10 | console.log(average(6, 20, 40)); // 22 -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/ends-with-t.js: -------------------------------------------------------------------------------- 1 | // Write a function `endsWithT` that accepts a string as an argument. The function should return a 2 | // boolean indicating whether or not the string ends with the character 't'. 3 | 4 | let endsWithT = function (str) { 5 | let lastChar = str[str.length - 1]; 6 | return lastChar === 't'; 7 | }; 8 | 9 | 10 | console.log(endsWithT("smart")); // true 11 | console.log(endsWithT("racket")); // true 12 | console.log(endsWithT("taco")); // false 13 | console.log(endsWithT("boomerang")); // false -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/half.js: -------------------------------------------------------------------------------- 1 | // Write a function `half` that accepts a number as an argument. The function should return half of the 2 | // number. 3 | 4 | let half = function (n) { 5 | return n / 2; 6 | }; 7 | 8 | console.log(half(8)); // 4 9 | console.log(half(15)); // 7.5 10 | console.log(half(90)); // 45 -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/is-div-by-4.js: -------------------------------------------------------------------------------- 1 | // Write a function `isDivBy4` that accepts a number as an argument. The function should return a 2 | // boolean indicating whether or not the number is divisible by 4. 3 | 4 | let isDivBy4 = function (num) { 5 | if (num % 4 === 0) { 6 | return true; 7 | } else { 8 | return false; 9 | } 10 | }; 11 | 12 | // let isDivBy4 = function (num) { 13 | // return num % 4 === 0; 14 | // }; 15 | 16 | console.log(isDivBy4(8)); // true 17 | console.log(isDivBy4(12)); // true 18 | console.log(isDivBy4(24)); // true 19 | console.log(isDivBy4(9)); // false 20 | console.log(isDivBy4(10)); // false -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/is-long.js: -------------------------------------------------------------------------------- 1 | // Write a function `isLong` that accepts a string as an argument. The function should return a boolean 2 | // indicating whether or not the string is longer than 5 characters 3 | 4 | let isLong = function (str) { 5 | return str.length > 5; 6 | }; 7 | 8 | // let isLong = function (str) { 9 | // if (str.length > 5) { 10 | // return true; 11 | // } else { 12 | // return false; 13 | // } 14 | // }; 15 | 16 | console.log(isLong("pie")); // false 17 | console.log(isLong("kite")); // false 18 | console.log(isLong("kitty")); // false 19 | console.log(isLong("telescope")); // true 20 | console.log(isLong("thermometer")); // true 21 | console.log(isLong("restaurant")); // true -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/keep-it-quiet.js: -------------------------------------------------------------------------------- 1 | // Write a function `keepItQuiet` that accepts a string as an argument. The function should return the 2 | // lowercase version of the string with 3 periods added to the end of it. 3 | 4 | let keepItQuiet = function (str) { 5 | return str.toLowerCase() + '...'; 6 | }; 7 | 8 | console.log(keepItQuiet("HOORAY")); // 'hooray...' 9 | console.log(keepItQuiet("Doggo")); // 'doggo...' 10 | console.log(keepItQuiet("WHAT?!?!")); // 'what?!?!...' -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/snippet-0-1.js: -------------------------------------------------------------------------------- 1 | // snippet 0-1 2 | let greet = function () { 3 | console.log("hey"); 4 | console.log("programmers"); 5 | }; 6 | 7 | let whistle = function () { 8 | console.log("doot"); 9 | }; 10 | 11 | console.log("first"); 12 | console.log("second"); 13 | greet(); 14 | console.log("third"); 15 | console.log("fourth"); 16 | whistle(); 17 | 18 | 19 | // first 20 | // second 21 | // hey 22 | // programmers 23 | // third 24 | // fourth 25 | // doot -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/snippet-0-2.js: -------------------------------------------------------------------------------- 1 | // snippet 0-2 2 | let howMany = function () { 3 | return 42; 4 | }; 5 | 6 | console.log(howMany); 7 | console.log(howMany()); 8 | 9 | const theAnswer = howMany(); 10 | console.log(theAnswer); 11 | 12 | let howMuch = function () { 13 | 5; 14 | }; 15 | console.log(howMuch()); 16 | 17 | 18 | // [Function: howMany] 19 | // 42 20 | // 42 21 | // undefined -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/snippet-0-3.js: -------------------------------------------------------------------------------- 1 | // snippet 0-3 2 | let average = function (num1, num2) { 3 | console.log("calculating..."); 4 | return (num1 + num2) / 2; 5 | }; 6 | 7 | console.log(average(5, 10)); 8 | console.log(average(20, 26)); 9 | console.log(average(50, 100) + 2); 10 | 11 | let a = 21 + 3; 12 | let b = 20; 13 | let n = average(a, b); 14 | console.log(average(n, 18)); 15 | 16 | 17 | // calculating... 18 | // 7.5 19 | // calculating... 20 | // 23 21 | // calculating... 22 | // 77 23 | // calculating... 24 | // calculating... 25 | // 20 -------------------------------------------------------------------------------- /02_functions/solutions/A_functions_solution/snippet-0-4.js: -------------------------------------------------------------------------------- 1 | // snippet 0-4 2 | let exclaim = function (str) { 3 | let capitalized = str.toUpperCase(); 4 | return capitalized + "!!"; 5 | }; 6 | 7 | let result = exclaim("potato"); 8 | console.log(result); 9 | console.log(result.length); 10 | console.log(result[0]); 11 | console.log(result[result.length - 1]); 12 | 13 | 14 | // POTATO!! 15 | // 8 16 | // P 17 | // ! -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/ends-in-ly.js: -------------------------------------------------------------------------------- 1 | // Write a function `endsInLy` that accepts a string as an argument and returns a boolean indicating 2 | // whether or not the string ends in the substring 'ly'. 3 | 4 | let endsInLy = function (str) { 5 | let secondLast = str[str.length - 2]; 6 | let last = str[str.length - 1]; 7 | 8 | return secondLast === 'l' && last === 'y'; 9 | }; 10 | 11 | // let endsInLy = function (str) { 12 | // return str.endsWith('ly'); 13 | // }; 14 | 15 | console.log(endsInLy("pretty")); // false 16 | console.log(endsInLy("instant")); // false 17 | console.log(endsInLy("analytic")); // false 18 | console.log(endsInLy("timidly")); // true 19 | console.log(endsInLy("fly")); // true 20 | console.log(endsInLy("gallantly")); // true -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/funny-sound.js: -------------------------------------------------------------------------------- 1 | // Write a function `funnySound` that accepts two strings as arguments. The function should return a 2 | // new string containing the first three characters of both strings concatenated together. 3 | 4 | // You can assume that the arguments are both at least three characters long. 5 | 6 | let funnySound = function (str1, str2) { 7 | return str1.slice(0, 3) + str2.slice(0, 3); 8 | }; 9 | 10 | console.log(funnySound("tiger", "spoon")); // 'tigspo' 11 | console.log(funnySound("computer", "phone")); // 'compho' 12 | console.log(funnySound("skate", "bottle")); // 'skabot' 13 | console.log(funnySound("frog", "ashtray")); // 'froash' -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/longer.js: -------------------------------------------------------------------------------- 1 | // Write a function `longer` that accepts two strings as arguments. The function should return the 2 | // string that is longer. If the strings have the same length, then return the first string. 3 | 4 | let longer = function (str1, str2) { 5 | if (str1.length >= str2.length) { 6 | return str1; 7 | } else { 8 | return str2; 9 | } 10 | }; 11 | 12 | console.log(longer("drum", "piranha")); // 'piranha' 13 | console.log(longer("basket", "fork")); // 'basket' 14 | console.log(longer("flannel", "sustainable")); // 'sustainable' 15 | console.log(longer("disrupt", "ability")); // 'disrupt' 16 | console.log(longer("bird", "shoe")); // 'bird' -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/one-or-none.js: -------------------------------------------------------------------------------- 1 | // Write a function `oneOrNone` that accepts two booleans as arguments. The function should return true 2 | // if exactly one of the arguments is true. If BOTH arguments are true, then it should return false. 3 | 4 | let oneOrNone = function (val1, val2) { 5 | if ((val1 || val2) && !(val1 && val2)) { 6 | return true; 7 | } else { 8 | return false; 9 | } 10 | }; 11 | 12 | console.log(oneOrNone(false, false)); // false 13 | console.log(oneOrNone(true, false)); // true 14 | console.log(oneOrNone(false, true)); // true 15 | console.log(oneOrNone(true, true)); // false -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/parity.js: -------------------------------------------------------------------------------- 1 | // Write a function `parity` that accepts a number as an argument. The function should return the 2 | // string 'even' if the number is even. It should return the string 'odd' if the number is odd. 3 | 4 | let parity = function (num) { 5 | if (num % 2 === 0) { 6 | return 'even'; 7 | } else { 8 | return 'odd'; 9 | } 10 | }; 11 | 12 | console.log(parity(5)); // 'odd' 13 | console.log(parity(7)); // 'odd' 14 | console.log(parity(13)); // 'odd' 15 | console.log(parity(32)); // 'even' 16 | console.log(parity(10)); // 'even' 17 | console.log(parity(602348)); // 'even' -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/starts-with-r.js: -------------------------------------------------------------------------------- 1 | // Write a function `startsWithR` that accepts a string as an argument and returns a boolean indicating 2 | // whether or not the string starts with 'r' or 'R'. 3 | 4 | let startsWithR = function (str) { 5 | return str[0] === 'r' || str[0] === 'R'; 6 | }; 7 | 8 | console.log(startsWithR("roger that")); // true 9 | console.log(startsWithR("Row, row, row your boat")); // true 10 | console.log(startsWithR("slip")); // false 11 | console.log(startsWithR("Taxicab")); // false -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/string-size.js: -------------------------------------------------------------------------------- 1 | // Write a function `stringSize` that accepts a string as an argument. The function should return the 2 | // string 'small' if the argument is shorter than 5 characters, 'medium' if it is exactly 5 characters, and 3 | // 'large' if it is longer than 5 characters. 4 | 5 | let stringSize = function (str) { 6 | if (str.length < 5) { 7 | return 'small'; 8 | } else if (str.length > 5) { 9 | return 'large'; 10 | } else { 11 | return 'medium'; 12 | } 13 | }; 14 | 15 | console.log(stringSize("cat")); // 'small' 16 | console.log(stringSize("bell")); // 'small' 17 | console.log(stringSize("ready")); // 'medium' 18 | console.log(stringSize("shirt")); // 'medium' 19 | console.log(stringSize("shallow")); // 'large' 20 | console.log(stringSize("intelligence")); // 'large' -------------------------------------------------------------------------------- /02_functions/solutions/B_functions_solution/wacky-word.js: -------------------------------------------------------------------------------- 1 | // Write a function `wackyWord` that accepts two strings as arguments. The function should return a new 2 | // string containing the first three characters of the first string concatenated with the last two 3 | // character of the second string. 4 | 5 | // You can assume that the first argument has a length of at least three and the second argument has a 6 | // length of at least two. 7 | 8 | 9 | let wackyWord = function (str1, str2) { 10 | return str1.slice(0, 3) + str2.slice(-2); 11 | }; 12 | 13 | console.log(wackyWord("very", "kindly")); // 'verly' 14 | console.log(wackyWord("forever", "sick")); // 'forck' 15 | console.log(wackyWord("cellar", "door")); // 'celor' 16 | console.log(wackyWord("bagel", "sweep")); // 'bagep' 17 | 18 | 19 | -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/average-of-four.js: -------------------------------------------------------------------------------- 1 | // Write a function `averageOfFour(num1, num2, num3, num4)` that accepts four numbers as arguments. The 2 | // function should return the average of all four numbers. 3 | 4 | let averageOfFour = function (num1, num2, num3, num4) { 5 | let sum = num1 + num2 + num3 + num4; 6 | return sum / 4; 7 | }; 8 | 9 | console.log(averageOfFour(10, 4, 12, 3)); // 7.25 10 | console.log(averageOfFour(-20, 50, 4, 21)); // 13.75 11 | console.log(averageOfFour(10, 4, 12, 3)); // 7.25 12 | console.log(averageOfFour(5, 5, 3, 7)); // 5 -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/case-changer.js: -------------------------------------------------------------------------------- 1 | // Write a function `caseChange` that accepts a string and a boolean as arguments. The function should 2 | // return the uppercase version of the string if the boolean is true. The function should return the 3 | // lowercase version of the string if the boolean is false. 4 | 5 | let caseChange = function (str, shouldUpperCase) { 6 | if (shouldUpperCase) { 7 | return str.toUpperCase(); 8 | } else { 9 | return str.toLowerCase(); 10 | } 11 | }; 12 | 13 | console.log(caseChange("Super", true)); // 'SUPER' 14 | console.log(caseChange("Super", false)); // 'super' 15 | console.log(caseChange("tAmBourine", true)); // 'TAMBOURINE' 16 | console.log(caseChange("tAmBourine", false)); // 'tambourine' -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/contains.js: -------------------------------------------------------------------------------- 1 | // Write a function `contains(str1, str2)` that accepts two strings as arguments. The function should 2 | // return a boolean indicating whether or not `str2` is contained within `str1`. The function should 3 | // ignore any differences in capitalization. 4 | 5 | let contains = function (str1, str2) { 6 | let lowerStr1 = str1.toLowerCase(); 7 | let lowerStr2 = str2.toLowerCase(); 8 | return lowerStr1.indexOf(lowerStr2) > -1; 9 | }; 10 | 11 | console.log(contains("caterpillar", "pill")); // true 12 | console.log(contains("lion's share", "on")); // true 13 | console.log(contains("SORRY", "or")); // true 14 | console.log(contains("tangent", "gem")); // false 15 | console.log(contains("clock", "ok")); // false -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/divisible.js: -------------------------------------------------------------------------------- 1 | // Write a function `divisible(num1, num2)` that accepts two numbers as arguments. The function should 2 | // return a boolean indicating whether or not `num1` is divisible by `num2`. 3 | 4 | let divisible = function (num1, num2) { 5 | return num1 % num2 === 0; 6 | }; 7 | 8 | // let divisible = function (num1, num2) { 9 | // if (num1 % num2 === 0) { 10 | // return true; 11 | // } else { 12 | // return false; 13 | // } 14 | // }; 15 | 16 | console.log(divisible(12, 3)); // true 17 | console.log(divisible(12, 5)); // false 18 | console.log(divisible(60, 4)); // true 19 | console.log(divisible(60, 11)); // false 20 | console.log(divisible(21, 7)); // true 21 | console.log(divisible(21, 6)); // false -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/in-range.js: -------------------------------------------------------------------------------- 1 | // Write a function `inRange(min, max, n)` that accepts three numbers as arguments. The function should 2 | // return a boolean indicating if `n` is between `min` and `max` inclusive. 3 | 4 | let inRange = function (min, max, n) { 5 | return n >= min && n <= max; 6 | }; 7 | 8 | console.log(inRange(5, 13, 8)); // true 9 | console.log(inRange(5, 13, 29)); // false 10 | console.log(inRange(100, 125, 100)); // true 11 | console.log(inRange(100, 125, 99)); // false 12 | console.log(inRange(40, 45, 44)); // true 13 | console.log(inRange(40, 45, 45)); // true 14 | console.log(inRange(40, 45, 46)); // false -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/larger.js: -------------------------------------------------------------------------------- 1 | // Write a function `larger` that accepts two numbers as arguments. The function should return the 2 | // larger number. 3 | 4 | let larger = function (num1, num2) { 5 | if (num1 > num2) { 6 | return num1; 7 | } else { 8 | return num2; 9 | } 10 | }; 11 | 12 | console.log(larger(256, 400)); // 400 13 | console.log(larger(31, 4)); // 31 14 | console.log(larger(-6, 7)); // 7 15 | console.log(larger(11.3, 11.2)); // 11.3 16 | console.log(larger(-10, -3)); // -3 -------------------------------------------------------------------------------- /02_functions/solutions/C_functions_solution/number-change.js: -------------------------------------------------------------------------------- 1 | // Write a function `numberChange` that accepts a number as an argument. The function should return 2 | // half the number if it is even. The function should return double the number if it is odd. 3 | 4 | let numberChange = function (n) { 5 | if (n % 2 === 0) { 6 | return n / 2; 7 | } else { 8 | return n * 2; 9 | } 10 | }; 11 | 12 | console.log(numberChange(6)); // 3 13 | console.log(numberChange(7)); // 14 14 | console.log(numberChange(16)); // 8 15 | console.log(numberChange(21)); // 42 -------------------------------------------------------------------------------- /03_loops/exercises/A_loops_exercise/README.md: -------------------------------------------------------------------------------- 1 | # In For a Loop 2 | 3 | _Loops_ are a construct that programmers use control the repeated execution of code. 4 | 5 | Before you start this exercise, make sure you have watched the lecture first. 6 | 7 | + [Lecture](https://youtu.be/gs4WlW-ZQfg) 8 | 9 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 10 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 11 | 12 | + [Walkthrough Part 1](https://youtu.be/D_Vh-Opw-_I) 13 | + [Walkthrough Part 2](https://youtu.be/rlolQAocQyA) 14 | 15 | I think loops are great because we can type a small amount of code, but instruct the computer to 16 | perform a lot of "work". The programmer is lazy and computer doesn't complain. What a perfect pair. 17 | 18 | -AZ 19 | 20 | ## 0. Read. Run. Repeat. 21 | 22 | Can you anticipate what these loop snippets will print out? 23 | 24 | Create a new folder named `A_loops_exercise`. Predict what would print when executing each of the 25 | snippets below. Test your prediction by typing the snippet into their own files and executing the 26 | program. Don't copy and paste. 27 | 28 | Write the below code into a file named `snippet-1.js`. Make a prediction for what it will print. Run 29 | it. 30 | 31 | ```js 32 | // snippet 1 33 | console.log("hello"); 34 | 35 | for (let i = 0; i < 5; i++) { 36 | console.log("code"); 37 | } 38 | 39 | console.log("goodbye"); 40 | ``` 41 | 42 | Write the below code into a file named `snippet-2.js`. Make a prediction for what it will print. Run 43 | it. 44 | 45 | ```js 46 | // snippet 2 47 | console.log("hi"); 48 | 49 | for (let i = 3; i <= 7; i++) { 50 | console.log("program"); 51 | console.log(i); 52 | } 53 | 54 | console.log("bye"); 55 | ``` 56 | 57 | Write the below code into a file named `snippet-3.js`. Make a prediction for what it will print. Run 58 | it. 59 | 60 | ```js 61 | // snippet 3 62 | let foo = function () { 63 | for (let num = 10; num > 0; num -= 2) { 64 | console.log(num); 65 | } 66 | }; 67 | 68 | console.log("begin"); 69 | foo(); 70 | console.log("end"); 71 | foo(); 72 | ``` 73 | 74 | Write the below code into a file named `snippet-4.js`. Make a prediction for what it will print. Run 75 | it. 76 | 77 | ```js 78 | // snippet 4 79 | let word = "street"; 80 | for (let i = 0; i < word.length; i++) { 81 | console.log(i); 82 | console.log(word[i]); 83 | } 84 | ``` 85 | 86 | Write the below code into a file named `snippet-5.js`. Make a prediction for what it will print. Run 87 | it. 88 | 89 | ```js 90 | // snippet 5 91 | let total = 0; 92 | for (let i = 1; i < 5; i++) { 93 | total += i; 94 | console.log(total); 95 | } 96 | 97 | console.log("grand total: " + total); 98 | ``` 99 | 100 | ## 1. Stay in the Loop. 101 | 102 | In this section of the exercise you'll be prompted to write a function to accomplish a specific 103 | task. Read the description and look at the example output to figure out what should be implemented 104 | within the function. Write each function to a separate file in the `A_loops_exercise` folder and 105 | test your work by running the file. 106 | 107 | ### oneToFour 108 | 109 | Create a `one-to-four.js` file and solve the function below: 110 | 111 | ```js 112 | // Write a function `oneToFour` that prints all whole numbers from one to four, inclusive. The function 113 | // takes in no arguments and doesn't need to return any value. It should just print to the terminal. 114 | 115 | oneToFour(); 116 | // prints 117 | // 1 118 | // 2 119 | // 3 120 | // 4 121 | ``` 122 | 123 | ### countUp 124 | 125 | Create a `count-up.js` file and solve the function below: 126 | 127 | ```js 128 | // Write a function `countUp(max)` that accepts a max number as an argument. The function should print 129 | // all numbers from 1 up to and including the max. The function doesn't need to return any value. It 130 | // should just print to the terminal. 131 | 132 | countUp(5); 133 | // prints 134 | // 1 135 | // 2 136 | // 3 137 | // 4 138 | // 5 139 | 140 | countUp(3); 141 | // prints 142 | // 1 143 | // 2 144 | // 3 145 | ``` 146 | 147 | ### minToMax 148 | 149 | Create a `min-to-max.js` file and solve the function below: 150 | 151 | ```js 152 | // Write a function `minToMax(min, max)` that accepts two numbers as arguments. The function should 153 | // print all numbers from min to max inclusive. The function doesn't need to return any value. It 154 | // should just print to the terminal. 155 | 156 | minToMax(5, 9); 157 | // prints 158 | // 5 159 | // 6 160 | // 7 161 | // 8 162 | // 9 163 | 164 | minToMax(11, 13); 165 | // prints 166 | // 11 167 | // 12 168 | // 13 169 | 170 | minToMax(20, 11); 171 | ``` 172 | 173 | ### stringIterate 174 | 175 | Create a `string-iterate.js` file and solve the function below: 176 | 177 | ```js 178 | // Write a function `stringIterate` that accepts a string as an argument. The function should print out 179 | // each character of the string, one by one. The function doesn't need to return any value. It should 180 | // just print to the terminal. 181 | 182 | stringIterate("celery"); 183 | // prints 184 | // c 185 | // e 186 | // l 187 | // e 188 | // r 189 | // y 190 | 191 | stringIterate("hat"); 192 | // prints 193 | // h 194 | // a 195 | // t 196 | ``` 197 | 198 | ### evens 199 | 200 | Create an `evens.js` file and solve the function below: 201 | 202 | ```js 203 | // Write a function `evens(max)` that accepts a max number as an argument. The function should print 204 | // all positive even numbers that are less than the max. 205 | 206 | evens(11); 207 | // prints 208 | // 2 209 | // 4 210 | // 6 211 | // 8 212 | // 10 213 | 214 | evens(8); 215 | // prints 216 | // 2 217 | // 4 218 | // 6 219 | ``` 220 | 221 | ## Walkthrough 222 | 223 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 224 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 225 | Watch it in 1.5x speed if you like. 226 | -------------------------------------------------------------------------------- /03_loops/exercises/B_loops_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Repetition Leads to Perfection 2 | 3 | More practice on `for` loops! 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | + [Walkthrough](https://youtu.be/7EbhmLAH__o) 9 | 10 | Roses are read, violets are blue, error on line 32. 11 | 12 | -AZ 13 | 14 | ## Instructions 15 | 16 | Create a new folder named `B_loops`, this is where you'll save all of your files. Solve each of the 17 | prompts below. Create a new file for each prompt and execute your code to see if it matches the 18 | example output. 19 | 20 | ### fiveMultiplesOf 21 | 22 | Create a `five-multiples-of.js` file and solve the function below: 23 | 24 | 25 | ```js 26 | // Write a function named `fiveMultiplesOf` that accepts a number as an argument. The function should 27 | // print out the first five multiples of the given number. The function doesn't need to return any 28 | // value. It should just print to the terminal. 29 | 30 | fiveMultiplesOf(7); 31 | // prints 32 | // 7 33 | // 14 34 | // 21 35 | // 28 36 | // 35 37 | 38 | fiveMultiplesOf(3); 39 | // prints 40 | // 3 41 | // 6 42 | // 9 43 | // 12 44 | // 15 45 | ``` 46 | 47 | ### sumUpTo 48 | 49 | Create a `sum-up-to.js` file and solve the function below: 50 | 51 | 52 | ```js 53 | // Write a function named `sumUpTo(max)` that accepts a max number as an argument. The function should 54 | // return the total sum of all whole numbers from 1 to the max, inclusive. 55 | // 56 | // For example, sumUpTo(4) should return 10 because 1 + 2 + 3 + 4 = 10. 57 | 58 | console.log(sumUpTo(4)); // 10 59 | console.log(sumUpTo(5)); // 15 60 | console.log(sumUpTo(2)); // 3 61 | ``` 62 | 63 | ### noOhs 64 | 65 | Create a `no-ohs.js` file and solve the function below: 66 | 67 | 68 | ```js 69 | // Write a function named `noOhs` that accepts a string as an argument. The functions should print the 70 | // characters of the string one by one except the character 'o'. The function doesn't need to return 71 | // any value. It should just print to the terminal. 72 | 73 | noOhs("code"); 74 | // prints 75 | // c 76 | // d 77 | // e 78 | 79 | noOhs("school"); 80 | // prints 81 | // s 82 | // c 83 | // h 84 | // l 85 | ``` 86 | 87 | ### oddSum 88 | 89 | Create an `odd-sum.js` file and solve the function below: 90 | 91 | ```js 92 | // Write a function named `oddSum(max)` that accepts a max number as an argument. The function should 93 | // return the total sum of all odd numbers from 1 to the max, inclusive. 94 | // 95 | // For example, oddSum(10) should return 25 because 1 + 3 + 5 + 7 + 9 = 25 96 | 97 | console.log(oddSum(10)); // 25 98 | console.log(oddSum(5)); // 9 99 | ``` 100 | 101 | ### stringRepeater 102 | 103 | Create a `string-repeater.js` file and solve the function below: 104 | 105 | 106 | ```js 107 | // Write a function named `stringRepeater(str, num)` that accepts a string and a number as arguments. 108 | // The function should return a new string consisting of the `str` repeated `num` number of times. 109 | 110 | console.log(stringRepeater("q", 4)); // 'qqqq' 111 | console.log(stringRepeater("go", 2)); // 'gogo' 112 | console.log(stringRepeater("tac", 3)); // 'tactactac' 113 | ``` 114 | 115 | ### productUpTo 116 | 117 | Create a `product-up-to.js` file and solve the function below: 118 | 119 | 120 | ```js 121 | // Write a function named `productUpTo(max)` that accepts a max number as an argument. The function 122 | // should return the total product of all whole numbers from 1 to the max, inclusive. A product is a 123 | // number obtained from multiplying numbers together. 124 | // 125 | // For example, productUpTo(4) should return 24 because 1 * 2 * 3 * 4 = 24 126 | 127 | console.log(productUpTo(4)); // 24 128 | console.log(productUpTo(5)); // 120 129 | console.log(productUpTo(7)); // 5040 130 | ``` 131 | 132 | ### divByEither 133 | 134 | Create a `div-by-either.js` file and solve the function below: 135 | 136 | 137 | ```js 138 | // Write a function named `divByEither(num1, num2, max)` that accepts three numbers as arguments. 139 | // The function should print out all positive numbers less than max that are divisible by num1 or num2. 140 | // The function doesn't need to return any value. It should just print to the terminal. 141 | 142 | divByEither(4, 3, 16); 143 | // prints 144 | // 3 145 | // 4 146 | // 6 147 | // 8 148 | // 9 149 | // 12 150 | // 15 151 | 152 | divByEither(7, 5, 20); 153 | // prints 154 | // 5 155 | // 7 156 | // 10 157 | // 14 158 | // 15 159 | ``` 160 | 161 | ## Walkthrough 162 | 163 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 164 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 165 | Watch it in 1.5x speed if you like. 166 | -------------------------------------------------------------------------------- /03_loops/exercises/C_loops_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Like a Broken Record 2 | 3 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 4 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 5 | 6 | + [Lecture](https://youtu.be/3uEzx16zwIM) 7 | 8 | You are getting good at solving problems using loops. Let's up the difficulty. 9 | 10 | -AZ 11 | 12 | ## Instructions 13 | 14 | Create a new folder named `C_loops`, this is where you'll save all of your files. Solve each of the 15 | prompts below. Create a new file for each prompt and execute your code to see if it matches the 16 | example output. 17 | 18 | ### divisibleRange 19 | 20 | Create a `divisible-range.js` file and solve the function below: 21 | 22 | ```js 23 | // Write a function `divisibleRange(min, max, num)` that accepts three numbers as arguments. The 24 | // function should print all numbers between `min` and `max` (exclusive) that are also divisible by 25 | // num. 26 | 27 | divisibleRange(17, 40, 9); 28 | // prints 29 | // 18 30 | // 27 31 | // 36 32 | 33 | divisibleRange(10, 24, 4); 34 | // prints 35 | // 12 36 | // 16 37 | // 20 38 | ``` 39 | 40 | ### reverseIterate 41 | 42 | Create a `reverse-iterate.js` file and solve the function below: 43 | 44 | ```js 45 | // Write a function `reverseIterate` that accepts a string as an argument. The function should print 46 | // the characters of the string one by one, in reverse order. The function doesn't need to return any 47 | // value. It should just print to the terminal. 48 | 49 | reverseIterate("carrot"); 50 | // prints 51 | // t 52 | // o 53 | // r 54 | // r 55 | // a 56 | // c 57 | 58 | reverseIterate("box"); 59 | // prints 60 | // x 61 | // o 62 | // b 63 | ``` 64 | 65 | ### removeCapitals 66 | 67 | Create a `remove-capitals.js` file and solve the function below: 68 | 69 | ```js 70 | // Write a function `removeCapitals` that accepts a string as an argument. The function should return a 71 | // new version of the string with all capital letters removed. 72 | 73 | console.log(removeCapitals("fOrEver")); // 'frver' 74 | console.log(removeCapitals("raiNCoat")); // 'raioat' 75 | console.log(removeCapitals("cElLAr Door")); // 'clr oor' 76 | ``` 77 | 78 | ### raisePower 79 | 80 | Create a `raise-power.js` file and solve the function below: 81 | 82 | ```js 83 | // Write a function `raisePower(base, exponent)` that accepts two numbers, `base` and `exponent`. The 84 | // function should return `base` raised to the `exponent` power. 85 | // 86 | // For example, raisePower(2, 5) should return 32 because 2 * 2 * 2 * 2 * 2 = 32 87 | // For example, raisePower(4, 3) should return 64 because 4 * 4 * 4 = 64 88 | 89 | console.log(raisePower(2, 5)); // 32 90 | console.log(raisePower(4, 3)); // 64 91 | console.log(raisePower(10, 4)); // 10000 92 | console.log(raisePower(7, 2)); // 49 93 | ``` 94 | 95 | ### censorE 96 | 97 | Create a `censor-e.js` file and solve the function below: 98 | 99 | ```js 100 | // Write a function `censorE` that accepts a string as an argument. The function should return the a new 101 | // version of string where all characters that are 'e's are replaced with '*'s. 102 | 103 | console.log(censorE("speedy")); // 'sp**dy' 104 | console.log(censorE("pending")); // 'p*nding' 105 | console.log(censorE("scene")); // 'sc*n*' 106 | console.log(censorE("heat")); // 'h*at' 107 | ``` 108 | 109 | ### fizzBuzz 110 | 111 | Create a `fizz-buzz.js` file and solve the function below: 112 | 113 | ```js 114 | // Write a function `fizzBuzz` that accepts a max number as an argument. The function should 115 | // print all numbers less than or equal to max that are divisible by either 3 or 5 but not both 3 116 | // and 5. The function doesn't need to return any value. It should just print to the terminal. 117 | 118 | fizzBuzz(18); 119 | // prints 120 | // 3 121 | // 5 122 | // 6 123 | // 9 124 | // 10 125 | // 12 126 | // 18 127 | 128 | fizzBuzz(33); 129 | // prints 130 | // 3 131 | // 5 132 | // 6 133 | // 9 134 | // 10 135 | // 12 136 | // 18 137 | // 20 138 | // 21 139 | // 24 140 | // 25 141 | // 27 142 | // 33 143 | ``` 144 | 145 | ## Walkthrough 146 | 147 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 148 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 149 | Watch it in 1.5x speed if you like. 150 | -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/count-up.js: -------------------------------------------------------------------------------- 1 | // Write a function `countUp(max)` that accepts a max number as an argument. The function should print 2 | // all numbers from 1 up to and including the max. The function doesn't need to return any value. It 3 | // should just print to the terminal. 4 | 5 | let countUp = function (max) { 6 | for (let i = 1; i <= max; i++) { 7 | console.log(i); 8 | } 9 | }; 10 | 11 | countUp(5); 12 | // prints 13 | // 1 14 | // 2 15 | // 3 16 | // 4 17 | // 5 18 | 19 | countUp(3); 20 | // prints 21 | // 1 22 | // 2 23 | // 3 -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/evens.js: -------------------------------------------------------------------------------- 1 | // Write a function `evens(max)` that accepts a max number as an argument. The function should print 2 | // all positive even numbers that are less than the max. 3 | 4 | let evens = function (max) { 5 | for (let i = 1; i < max; i++) { 6 | if (i % 2 === 0) { 7 | console.log(i); 8 | } 9 | } 10 | }; 11 | 12 | evens(11); 13 | // prints 14 | // 2 15 | // 4 16 | // 6 17 | // 8 18 | // 10 19 | 20 | evens(8); 21 | // prints 22 | // 2 23 | // 4 24 | // 6 -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/min-to-max.js: -------------------------------------------------------------------------------- 1 | // Write a function `minToMax(min, max)` that accepts two numbers as arguments. The function should 2 | // print all numbers from min to max inclusive. The function doesn't need to return any value. It 3 | // should just print to the terminal. 4 | 5 | 6 | let minToMax = function (min, max) { 7 | for (let i = min; i <= max; i++) { 8 | console.log(i); 9 | } 10 | }; 11 | 12 | minToMax(5, 9); 13 | // prints 14 | // 5 15 | // 6 16 | // 7 17 | // 8 18 | // 9 19 | 20 | minToMax(11, 13); 21 | // prints 22 | // 11 23 | // 12 24 | // 13 25 | -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/one-to-four.js: -------------------------------------------------------------------------------- 1 | // Write a function `oneToFour` that prints all whole numbers from one to four, inclusive. The function 2 | // takes in no arguments and doesn't need to return any value. It should just print to the terminal. 3 | 4 | let oneToFour = function () { 5 | for (let i = 1; i <= 4; i++) { 6 | console.log(i); 7 | } 8 | }; 9 | 10 | 11 | oneToFour(); 12 | // prints 13 | // 1 14 | // 2 15 | // 3 16 | // 4 17 | -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/snippet-1.js: -------------------------------------------------------------------------------- 1 | // snippet 1 2 | console.log("hello"); 3 | 4 | for (let i = 0; i < 5; i++) { 5 | console.log("code"); 6 | } 7 | 8 | console.log("goodbye"); 9 | 10 | 11 | hello 12 | code 13 | code 14 | code 15 | code 16 | code 17 | goodbye -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/snippet-2.js: -------------------------------------------------------------------------------- 1 | // snippet 2 2 | console.log("hi"); 3 | 4 | for (let i = 3; i <= 7; i++) { 5 | console.log("program"); 6 | console.log(i); 7 | } 8 | 9 | console.log("bye"); 10 | 11 | 12 | // hi 13 | // program 14 | // 3 15 | // program 16 | // 4 17 | // program 18 | // 5 19 | // program 20 | // 6 21 | // program 22 | // 7 23 | // bye -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/snippet-3.js: -------------------------------------------------------------------------------- 1 | // snippet 3 2 | let foo = function () { 3 | for (let num = 10; num > 0; num -= 2) { 4 | console.log(num); 5 | } 6 | }; 7 | 8 | console.log("begin"); 9 | foo(); 10 | console.log("end"); 11 | foo(); 12 | 13 | 14 | // begin 15 | // 10 16 | // 8 17 | // 6 18 | // 4 19 | // 2 20 | // end 21 | // 10 22 | // 8 23 | // 6 24 | // 4 25 | // 2 -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/snippet-4.js: -------------------------------------------------------------------------------- 1 | // snippet 4 2 | let word = "street"; 3 | for (let i = 0; i < word.length; i++) { 4 | console.log(i); 5 | console.log(word[i]); 6 | } 7 | 8 | 9 | // 0 10 | // s 11 | // 1 12 | // t 13 | // 2 14 | // r 15 | // 3 16 | // e 17 | // 4 18 | // e 19 | // 5 20 | // t -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/snippet-5.js: -------------------------------------------------------------------------------- 1 | // snippet 5 2 | let total = 0; // 10 3 | for (let i = 1; i < 5; i++) { 4 | total += i; 5 | console.log(total); 6 | } 7 | 8 | console.log("grand total: " + total); 9 | 10 | 11 | // 1 12 | // 3 13 | // 6 14 | // 10 15 | // grand total: 10 -------------------------------------------------------------------------------- /03_loops/solutions/A_loops_solution/string-iterate.js: -------------------------------------------------------------------------------- 1 | // Write a function `stringIterate` that accepts a string as an argument. The function should print out 2 | // each character of the string, one by one. The function doesn't need to return any value. It should 3 | // just print to the terminal. 4 | 5 | let stringIterate = function (string) { 6 | for (let i = 0; i < string.length; i++) { 7 | console.log(string[i]); 8 | } 9 | }; 10 | 11 | stringIterate("celery"); 12 | // prints 13 | // c 14 | // e 15 | // l 16 | // e 17 | // r 18 | // y 19 | 20 | stringIterate("hat"); 21 | // prints 22 | // h 23 | // a 24 | // t -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/div-by-either.js: -------------------------------------------------------------------------------- 1 | // Write a function named `divByEither(num1, num2, max)` that accepts three numbers as arguments. 2 | // The function should print out all positive numbers less than max that are divisible by num1 or num2. 3 | // The function doesn't need to return any value. It should just print to the terminal. 4 | 5 | let divByEither = function (num1, num2, max) { 6 | for (let i = 1; i < max; i++) { 7 | if (i % num1 === 0 || i % num2 === 0) { 8 | console.log(i); 9 | } 10 | } 11 | }; 12 | 13 | divByEither(4, 3, 16); 14 | // prints 15 | // 3 16 | // 4 17 | // 6 18 | // 8 19 | // 9 20 | // 12 21 | // 15 22 | 23 | divByEither(7, 5, 20); 24 | // prints 25 | // 5 26 | // 7 27 | // 10 28 | // 14 29 | // 15 -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/five-multiples-of.js: -------------------------------------------------------------------------------- 1 | // Write a function named `fiveMultiplesOf` that accepts a number as an argument. The function should 2 | // print out the first five multiples of the given number. The function doesn't need to return any 3 | // value. It should just print to the terminal. 4 | 5 | let fiveMultiplesOf = function (num) { 6 | for (let i = 1; i <= 5; i++) { 7 | console.log(i * num); 8 | } 9 | }; 10 | 11 | fiveMultiplesOf(7); 12 | // prints 13 | // 7 14 | // 14 15 | // 21 16 | // 28 17 | // 35 18 | 19 | fiveMultiplesOf(3); 20 | // prints 21 | // 3 22 | // 6 23 | // 9 24 | // 12 25 | // 15 -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/no-ohs.js: -------------------------------------------------------------------------------- 1 | // Write a function named `noOhs` that accepts a string as an argument. The functions should print the 2 | // characters of the string one by one except the character 'o'. The function doesn't need to return 3 | // any value. It should just print to the terminal. 4 | 5 | let noOhs = function (str) { 6 | for (let i = 0; i < str.length; i++) { 7 | let char = str[i]; 8 | if (char !== 'o') { 9 | console.log(char); 10 | } 11 | } 12 | }; 13 | 14 | noOhs("code"); 15 | // prints 16 | // c 17 | // d 18 | // e 19 | 20 | noOhs("school"); 21 | // prints 22 | // s 23 | // c 24 | // h 25 | // l -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/odd-sum.js: -------------------------------------------------------------------------------- 1 | // Write a function named `oddSum(max)` that accepts a max number as an argument. The function should 2 | // return the total sum of all odd numbers from 1 to the max, inclusive. 3 | // 4 | // For example, oddSum(10) should return 25 because 1 + 3 + 5 + 7 + 9 = 25 5 | 6 | let oddSum = function (max) { 7 | let sum = 0; 8 | 9 | for (let i = 1; i <= max; i++) { 10 | if (i % 2 === 1) { 11 | sum += i; 12 | } 13 | } 14 | 15 | return sum; 16 | }; 17 | 18 | console.log(oddSum(10)); // 25 19 | console.log(oddSum(5)); // 9 -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/product-up-to.js: -------------------------------------------------------------------------------- 1 | // Write a function named `productUpTo(max)` that accepts a max number as an argument. The function 2 | // should return the total product of all whole numbers from 1 to the max, inclusive. A product is a 3 | // number obtained from multiplying numbers together. 4 | // 5 | // For example, productUpTo(4) should return 24 because 1 * 2 * 3 * 4 = 24 6 | 7 | let productUpTo = function (max) { 8 | let product = 1; 9 | 10 | for (let i = 1; i <= max; i++) { 11 | product *= i; 12 | } 13 | 14 | return product; 15 | }; 16 | 17 | console.log(productUpTo(4)); // 24 18 | console.log(productUpTo(5)); // 120 19 | console.log(productUpTo(7)); // 5040 -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/string-repeater.js: -------------------------------------------------------------------------------- 1 | // Write a function named `stringRepeater(str, num)` that accepts a string and a number as arguments. 2 | // The function should return a new string consisting of the `str` repeated `num` number of times. 3 | 4 | let stringRepeater = function (str, num) { 5 | let repeatedStr = ''; 6 | 7 | for (let i = 1; i <= num; i++) { 8 | repeatedStr += str 9 | } 10 | 11 | return repeatedStr; 12 | }; 13 | 14 | console.log(stringRepeater("q", 4)); // 'qqqq' 15 | console.log(stringRepeater("go", 2)); // 'gogo' 16 | console.log(stringRepeater("tac", 3)); // 'tactactac' -------------------------------------------------------------------------------- /03_loops/solutions/B_loops_solution/sum-up-to.js: -------------------------------------------------------------------------------- 1 | // Write a function named `sumUpTo(max)` that accepts a max number as an argument. The function should 2 | // return the total sum of all whole numbers from 1 to the max, inclusive. 3 | // 4 | // For example, sumUpTo(4) should return 10 because 1 + 2 + 3 + 4 = 10. 5 | 6 | let sumUpTo = function (max) { 7 | let sum = 0; 8 | 9 | for (let i = 1; i <= max; i++) { 10 | sum += i 11 | } 12 | 13 | return sum; 14 | }; 15 | 16 | console.log(sumUpTo(4)); // 10 17 | console.log(sumUpTo(5)); // 15 18 | console.log(sumUpTo(2)); // 3 -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/censor-e.js: -------------------------------------------------------------------------------- 1 | // Write a function `censorE` that accepts a string as an argument. The function should return the a new 2 | // version of string where all characters that are 'e's are replaced with '*'s. 3 | 4 | let censorE = function (str) { 5 | let newStr = ''; 6 | 7 | for (let i = 0; i < str.length; i++) { 8 | let char = str[i]; 9 | 10 | if (char === 'e') { 11 | newStr += '*'; 12 | } else { 13 | newStr += char; 14 | } 15 | } 16 | 17 | return newStr; 18 | }; 19 | 20 | console.log(censorE("speedy")); // 'sp**dy' 21 | console.log(censorE("pending")); // 'p*nding' 22 | console.log(censorE("scene")); // 'sc*n*' 23 | console.log(censorE("heat")); // 'h*at' -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/divisible-range.js: -------------------------------------------------------------------------------- 1 | // Write a function `divisibleRange(min, max, num)` that accepts three numbers as arguments. The 2 | // function should print all numbers between `min` and `max` (exclusive) that are also divisible by 3 | // num. 4 | 5 | let divisibleRange = function (min, max, num) { 6 | for (let i = min + 1; i < max; i++) { 7 | if (i % num === 0) { 8 | console.log(i); 9 | } 10 | } 11 | }; 12 | 13 | divisibleRange(17, 40, 9); 14 | // prints 15 | // 18 16 | // 27 17 | // 36 18 | 19 | divisibleRange(10, 24, 4); 20 | // prints 21 | // 12 22 | // 16 23 | // 20 -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/fizz-buzz.js: -------------------------------------------------------------------------------- 1 | // Write a function `fizzBuzz` that accepts a max number as an argument. The function should 2 | // print all numbers less than or equal to max that are divisible by either 3 or 5 but not both 3 3 | // and 5. The function doesn't need to return any value. It should just print to the terminal. 4 | 5 | let fizzBuzz = function (max) { 6 | for (let i = 1; i <= max; i++) { 7 | if ((i % 3 === 0 || i % 5 === 0) && !(i % 3 === 0 && i % 5 === 0)) { 8 | console.log(i); 9 | } 10 | } 11 | }; 12 | 13 | fizzBuzz(18); 14 | // prints 15 | // 3 16 | // 5 17 | // 6 18 | // 9 19 | // 10 20 | // 12 21 | // 18 22 | 23 | fizzBuzz(33); 24 | // prints 25 | // 3 26 | // 5 27 | // 6 28 | // 9 29 | // 10 30 | // 12 31 | // 18 32 | // 20 33 | // 21 34 | // 24 35 | // 25 36 | // 27 37 | // 33 -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/raise-power.js: -------------------------------------------------------------------------------- 1 | // Write a function `raisePower(base, exponent)` that accepts two numbers, `base` and `exponent`. The 2 | // function should return `base` raised to the `exponent` power. 3 | // 4 | // For example, raisePower(2, 5) should return 32 because 2 * 2 * 2 * 2 * 2 = 32 5 | // For example, raisePower(4, 3) should return 64 because 4 * 4 * 4 = 64 6 | 7 | let raisePower = function (base, exponent) { 8 | let product = 1; 9 | 10 | for (let i = 1; i <= exponent; i++) { 11 | product *= base; 12 | } 13 | 14 | return product; 15 | }; 16 | 17 | console.log(raisePower(2, 5)); // 32 18 | console.log(raisePower(4, 3)); // 64 19 | console.log(raisePower(10, 4)); // 10000 20 | console.log(raisePower(7, 2)); // 49 -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/remove-capitals.js: -------------------------------------------------------------------------------- 1 | // Write a function `removeCapitals` that accepts a string as an argument. The function should return a 2 | // new version of the string with all capital letters removed. 3 | 4 | let removeCapitals = function (str) { 5 | let newStr = ''; 6 | 7 | for (let i = 0; i < str.length; i++) { 8 | let char = str[i]; 9 | if (char === char.toLowerCase()) { 10 | newStr += char; 11 | } 12 | } 13 | 14 | return newStr; 15 | }; 16 | 17 | console.log(removeCapitals("fOrEver")); // 'frver' 18 | console.log(removeCapitals("raiNCoat")); // 'raioat' 19 | console.log(removeCapitals("cElLAr Door")); // 'clr oor' -------------------------------------------------------------------------------- /03_loops/solutions/C_loops_solution/reverse-iterate.js: -------------------------------------------------------------------------------- 1 | // Write a function `reverseIterate` that accepts a string as an argument. The function should print 2 | // the characters of the string one by one, in reverse order. The function doesn't need to return any 3 | // value. It should just print to the terminal. 4 | 5 | let reverseIterate = function (str) { 6 | for (let i = str.length - 1; i >= 0; i--) { 7 | console.log(str[i]); 8 | } 9 | }; 10 | 11 | reverseIterate("carrot"); 12 | // prints 13 | // t 14 | // o 15 | // r 16 | // r 17 | // a 18 | // c 19 | 20 | reverseIterate("box"); 21 | // prints 22 | // x 23 | // o 24 | // b -------------------------------------------------------------------------------- /04_arrays/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/04_arrays/.DS_Store -------------------------------------------------------------------------------- /04_arrays/exercises/A_arrays_exercise/README.md: -------------------------------------------------------------------------------- 1 | # All Programmers Need a Raise 2 | 3 | _Arrays_ are a construct that programmers use to store related values in a single variable. An 4 | _array_ contains one or more _elements_. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture](https://youtu.be/ekVhX_HfbBE) 9 | 10 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 11 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 12 | 13 | + [Walkthrough](https://youtu.be/mibav-eq9EY) 14 | 15 | New programmers often ask why the first index is 0 and not 1. Let's go half on it. 0.5? 16 | 17 | -AZ 18 | 19 | ## Instructions 20 | 21 | Create a new folder named `A_arrays`, this is where you'll save all of your files. Solve each of the 22 | prompts below. Create a new file for each prompt and execute your code to see if it matches the 23 | example output. 24 | 25 | ### total 26 | 27 | Create a `total.js` file and solve the function below: 28 | 29 | ```js 30 | // Write a function `total` that accepts an array of numbers as an argument. The function should return 31 | // the total sum of all elements of the array. 32 | 33 | console.log(total([3, 2, 8])); // 13 34 | console.log(total([-5, 7, 4, 6])); // 12 35 | console.log(total([7])); // 7 36 | console.log(total([])); // 0 37 | ``` 38 | 39 | ### stayPositive 40 | 41 | Create a `stay-positive.js` file and solve the function below: 42 | 43 | ```js 44 | // Write a function `stayPositive` that accepts an array of numbers as an argument. The function should 45 | // return an array containing only the positive numbers. 46 | 47 | console.log(stayPositive([10, -4, 3, 6])); // [10, 3, 6] 48 | console.log(stayPositive([-5, 11, -40, 30.3, -2])); // [11, 30.3] 49 | console.log(stayPositive([-11, -30])); // [] 50 | ``` 51 | 52 | ### bleepVowels 53 | 54 | Create a `bleep-vowels.js` file and solve the function below: 55 | 56 | ```js 57 | // Write a function `bleepVowels` that accepts a string as an argument. The function should return 58 | // a new string where all vowels are replaced with `*`s. Vowels are the letters a, e, i, o, u. 59 | 60 | console.log(bleepVowels("skateboard")); // 'sk*t*b**rd' 61 | console.log(bleepVowels("slipper")); // 'sl*pp*r' 62 | console.log(bleepVowels("range")); // 'r*ng*' 63 | console.log(bleepVowels("brisk morning")); // 'br*sk m*rn*ng' 64 | ``` 65 | 66 | ### filterLongWords 67 | 68 | Create a `filter-long-words.js` file and solve the function below: 69 | 70 | ```js 71 | // Write a function `filterLongWords` that accepts an array of strings as an argument. The function 72 | // should return a new array containing only the strings that are less than 5 characters long. 73 | 74 | console.log(filterLongWords(["kale", "cat", "retro", "axe", "heirloom"])); 75 | // ['kale', 'cat', 'axe'] 76 | 77 | console.log(filterLongWords(["disrupt", "pour", "trade", "pic"])); 78 | // ['pour', 'pic'] 79 | ``` 80 | 81 | ### numOdds 82 | 83 | Create a `num-odds.js` file and solve the function below: 84 | 85 | ```js 86 | // Write a function `numOdds` that accepts an array of numbers as an argument. The function should 87 | // return a number representing the count of odd elements in the array. 88 | 89 | console.log(numOdds([4, 7, 2, 5, 9])); // 3 90 | console.log(numOdds([11, 31, 58, 99, 21, 60])); // 4 91 | console.log(numOdds([100, 40, 4])); // 0 92 | ``` 93 | 94 | ### stringsToLengths 95 | 96 | Create a `strings-to-lengths.js` file and solve the function below: 97 | 98 | ```js 99 | // Write a function `stringsToLengths` that accepts an array of strings as an argument. The function 100 | // should return a new array containing the lengths of the elements of the original array. 101 | 102 | console.log(stringsToLengths(["belly", "echo", "irony", "pickled"])); 103 | // [5, 4, 5, 7] 104 | 105 | console.log(stringsToLengths(["on", "off", "handmade"])); 106 | // [2, 3, 8] 107 | ``` 108 | 109 | ### divisors 110 | 111 | Create a `divisors.js` file and solve the function below: 112 | 113 | ```js 114 | // Write a function `divisors` that accepts a number as an argument. The function should return an 115 | // array containing all positive numbers that can divide into the argument. 116 | 117 | console.log(divisors(15)); // [1, 3, 5, 15] 118 | console.log(divisors(7)); // [1, 7] 119 | console.log(divisors(24)); // [1, 2, 3, 4, 6, 8, 12, 24] 120 | ``` 121 | 122 | ## Walkthrough 123 | 124 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 125 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 126 | Watch it in 1.5x speed if you like. 127 | -------------------------------------------------------------------------------- /04_arrays/exercises/B_arrays_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Arrays for Days 2 | 3 | More array practice! 4 | 5 | Before you start this exercise, make sure you have watched the lecture first. 6 | 7 | + [Lecture](https://youtu.be/M4OpbSGGnjw) 8 | 9 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 10 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 11 | 12 | + [Walkthrough](https://youtu.be/l6dwZkBfi08) 13 | 14 | You may have noticed we added a few new patterns and techniques to our toolbox. The only way to get 15 | familiar with your tools is to use them. Write code. Run it. 16 | 17 | -AZ 18 | 19 | ## Instructions 20 | 21 | Create a new folder named `B_arrays`, this is where you'll save all of your files. Solve each of the 22 | prompts below. Create a new file for each prompt and execute your code to see if it matches the 23 | example output. 24 | 25 | ### makeAcronym 26 | 27 | Create a `make-acronym.js` file and solve the function below: 28 | 29 | ```js 30 | // Write a function `makeAcronym` that accepts a sentence string as an argument. The function should 31 | // return a string containing the first character of each word in the sentence. 32 | 33 | console.log(makeAcronym("New York")); // NY 34 | console.log(makeAcronym("same stuff different day")); // SSDD 35 | console.log(makeAcronym("Laugh out loud")); // LOL 36 | console.log(makeAcronym("don't over think stuff")); // DOTS 37 | ``` 38 | 39 | ### reverseArray 40 | 41 | Create a `reverse-array.js` file and solve the function below: 42 | 43 | ```js 44 | // Write a function `reverseArray` that accepts an array as an argument. The function should return a 45 | // array containing the elements of the original array in reverse order. 46 | 47 | console.log(reverseArray(["zero", "one", "two", "three"])); // ['three', 'two', 'one', 'zero'] 48 | console.log(reverseArray([7, 1, 8])); // [8, 1, 7] 49 | ``` 50 | 51 | ### yourAverageFunction 52 | 53 | Create a `your-average-function.js` file and solve the function below: 54 | 55 | ```js 56 | // Write a function `yourAverageFunction` that accepts an array of numbers as an argument. The 57 | // function should return the average of all elements of the array. If the input array is empty, 58 | // then the function should return null. 59 | 60 | console.log(yourAverageFunction([5, 2, 7, 24])); // 9.5 61 | console.log(yourAverageFunction([100, 6])); // 53 62 | console.log(yourAverageFunction([31, 32, 40, 12, 33])); // 29.6 63 | console.log(yourAverageFunction([])); // null 64 | ``` 65 | 66 | ### chooseDivisibles 67 | 68 | Create a `choose-divisibles.js` file and solve the function below: 69 | 70 | ```js 71 | // Write a function `chooseDivisibles(numbers, target)` that accepts an array of numbers and a 72 | // target number as arguments. The function should return an array containing elements of the original 73 | // array that are divisible by the target. 74 | 75 | console.log(chooseDivisibles([40, 7, 22, 20, 24], 4)); // [40, 20, 24] 76 | console.log(chooseDivisibles([9, 33, 8, 17], 3)); // [9, 33] 77 | console.log(chooseDivisibles([4, 25, 1000], 10)); // [1000] 78 | ``` 79 | 80 | ### maximum 81 | 82 | Create a `maximum.js` file and solve the function below: 83 | 84 | ```js 85 | // Write a function `maximum` that accepts an array of numbers as an argument. The function should 86 | // return the largest number of the array. If the array is empty, then the function should return null. 87 | 88 | console.log(maximum([5, 6, 3, 7])); // 7 89 | console.log(maximum([17, 15, 19, 11, 2])); // 19 90 | console.log(maximum([])); // null 91 | ``` 92 | 93 | ### wordCount 94 | 95 | Create a `word-count.js` file and solve the function below: 96 | 97 | ```js 98 | // Write a function `wordCount(sentence, targetWords)` that accepts a sentence string and an array of 99 | // `targetWords`. The function should return a count of the number of words of the sentence that are 100 | // in `targetWords`. 101 | 102 | console.log(wordCount("open the window please", ["please", "open", "sorry"])); // 2 103 | console.log(wordCount("drive to the cinema", ["the", "driver"])); // 1 104 | console.log(wordCount("can I have that can", ["can", "I"])); // 3 105 | ``` 106 | 107 | ## Walkthrough 108 | 109 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 110 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 111 | Watch it in 1.5x speed if you like. 112 | -------------------------------------------------------------------------------- /04_arrays/exercises/C_arrays_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Array Buffet 2 | 3 | Let's do one more array exercise before we hit the next topic. 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | + [Walkthrough](https://youtu.be/Whk1gGafY78) 9 | 10 | Remember that time at the beginning of the course where you couldn't read or write any code? Look at 11 | you now, smarty pants. 12 | 13 | -AZ 14 | 15 | ## Instructions 16 | 17 | Create a new folder named `C_arrays`, this is where you'll save all of your files. Solve each of the 18 | prompts below. Create a new file for each prompt and execute your code to see if it matches the 19 | example output. 20 | 21 | ### lengthiestWord 22 | 23 | Create a `lengthiest-word.js` file and solve the function below: 24 | 25 | ```js 26 | // Write a function `lengthiestWord` that accepts a sentence string as an argument. The function should 27 | // return the longest word of the sentence. If there is a tie, return the word that appears later 28 | // in the sentence. 29 | 30 | console.log(lengthiestWord("I am pretty hungry")); // 'hungry' 31 | console.log(lengthiestWord("we should think outside of the box")); // 'outside' 32 | console.log(lengthiestWord("down the rabbit hole")); // 'rabbit' 33 | console.log(lengthiestWord("simmer down")); // 'simmer' 34 | ``` 35 | 36 | ### alternatingCaps 37 | 38 | Create an `alternating-caps.js` file and solve the function below: 39 | 40 | ```js 41 | // Write a function `alternatingCaps` that accepts a sentence string as an argument. The function should 42 | // return the sentence where words alternate between lowercase and uppercase. 43 | 44 | console.log(alternatingCaps("take them to school")); // 'take THEM to SCHOOL' 45 | console.log(alternatingCaps("What did ThEy EAT before?")); // 'what DID they EAT before?' 46 | ``` 47 | 48 | ### numberRange 49 | 50 | Create a `number-range.js` file and solve the function below: 51 | 52 | ```js 53 | // Write a function `numberRange(min, max, step)` that accepts three numbers as arguments, `min`, 54 | // `max`, and `step`. The function should return all numbers between `min` and `max` at `step` intervals. 55 | // `min` and `max` are inclusive. 56 | 57 | console.log(numberRange(10, 40, 5)); // [10, 15, 20, 25, 30, 35, 40] 58 | console.log(numberRange(14, 24, 3)); // [14, 17, 20, 23] 59 | console.log(numberRange(8, 35, 6)); // [8, 14, 20, 26, 32] 60 | ``` 61 | 62 | ### removeShortWords 63 | 64 | Create a `remove-short-words.js` file and solve the function below: 65 | 66 | ```js 67 | // Write a function `removeShortWords` that accepts a sentence string as an argument. The function 68 | // should return a new sentence where all of the words shorter than 4 characters are removed. 69 | 70 | console.log(removeShortWords("knock on the door will you")); // 'knock door will' 71 | console.log(removeShortWords("a terrible plan")); // 'terrible plan' 72 | console.log(removeShortWords("run faster that way")); // 'faster that' 73 | ``` 74 | 75 | ### commonElements 76 | 77 | Create a `common-elements.js` file and solve the function below: 78 | 79 | ```js 80 | // Write a function `commonElements` that accepts two arrays as arguments. The function should return 81 | // a new array containing the elements that are found in both of the input arrays. The order of 82 | // the elements in the output array doesn't matter as long as the function returns the correct elements. 83 | 84 | let arr1 = ["a", "c", "d", "b"]; 85 | let arr2 = ["b", "a", "y"]; 86 | console.log(commonElements(arr1, arr2)); // ['a', 'b'] 87 | 88 | let arr3 = [4, 7]; 89 | let arr4 = [32, 7, 1, 4]; 90 | console.log(commonElements(arr3, arr4)); // [4, 7] 91 | ``` 92 | 93 | ## Walkthrough 94 | 95 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 96 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 97 | Watch it in 1.5x speed if you like. 98 | -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/bleep-vowels.js: -------------------------------------------------------------------------------- 1 | // Write a function `bleepVowels` that accepts a string as an argument. The function should return 2 | // a new string where all vowels are replaced with `*`s. Vowels are the letters a, e, i, o, u. 3 | 4 | let bleepVowels = function (str) { 5 | let vowels = ["a", "e", "i", "o", "u"]; 6 | let bleeped = ""; 7 | 8 | for (let i = 0; i < str.length; i++) { 9 | let char = str[i]; 10 | 11 | if (vowels.indexOf(char) > -1) { 12 | bleeped += "*"; 13 | } else { 14 | bleeped += char; 15 | } 16 | } 17 | 18 | return bleeped; 19 | }; 20 | 21 | console.log(bleepVowels("skateboard")); // 'sk*t*b**rd' 22 | console.log(bleepVowels("slipper")); // 'sl*pp*r' 23 | console.log(bleepVowels("range")); // 'r*ng*' 24 | console.log(bleepVowels("brisk morning")); // 'br*sk m*rn*ng' 25 | -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/divisors.js: -------------------------------------------------------------------------------- 1 | // Write a function `divisors` that accepts a number as an argument. The function should return an 2 | // array containing all positive numbers that can divide into the argument. 3 | 4 | let divisors = function (n) { 5 | let numbers = []; 6 | 7 | for (let i = 1; i <= n; i++) { 8 | if (n % i === 0) { 9 | numbers.push(i); 10 | } 11 | } 12 | 13 | return numbers; 14 | }; 15 | 16 | console.log(divisors(15)); // [1, 3, 5, 15] 17 | console.log(divisors(7)); // [1, 7] 18 | console.log(divisors(24)); // [1, 2, 3, 4, 6, 8, 12, 24] -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/filter-long-words.js: -------------------------------------------------------------------------------- 1 | // Write a function `filterLongWords` that accepts an array of strings as an argument. The function 2 | // should return a new array containing only the strings that are less than 5 characters long. 3 | 4 | let filterLongWords = function (words) { 5 | let chosenWords = []; 6 | 7 | for (let i = 0; i < words.length; i++) { 8 | let word = words[i]; 9 | 10 | if (word.length < 5) { 11 | chosenWords.push(word); 12 | } 13 | } 14 | 15 | return chosenWords; 16 | }; 17 | 18 | console.log(filterLongWords(["kale", "cat", "retro", "axe", "heirloom"])); 19 | // ['kale', 'cat', 'axe'] 20 | 21 | console.log(filterLongWords(["disrupt", "pour", "trade", "pic"])); 22 | // ['pour', 'pic'] -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/num-odds.js: -------------------------------------------------------------------------------- 1 | // Write a function `numOdds` that accepts an array of numbers as an argument. The function should 2 | // return a number representing the count of odd elements in the array. 3 | 4 | let numOdds = function (numbers) { 5 | let count = 0; 6 | 7 | for (let i = 0; i < numbers.length; i++) { 8 | if (numbers[i] % 2 === 1) { 9 | count += 1; 10 | } 11 | } 12 | 13 | return count; 14 | }; 15 | 16 | console.log(numOdds([4, 7, 2, 5, 9])); // 3 17 | console.log(numOdds([11, 31, 58, 99, 21, 60])); // 4 18 | console.log(numOdds([100, 40, 4])); // 0 -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/stay-positive.js: -------------------------------------------------------------------------------- 1 | // Write a function `stayPositive` that accepts an array of numbers as an argument. The function should 2 | // return an array containing only the positive numbers. 3 | 4 | let stayPositive = function (numbers) { 5 | let positives = []; 6 | 7 | for (let i = 0; i < numbers.length; i++) { 8 | if (numbers[i] > 0) { 9 | positives.push(numbers[i]); 10 | } 11 | } 12 | 13 | return positives; 14 | }; 15 | 16 | 17 | console.log(stayPositive([10, -4, 3, 6])); // [10, 3, 6] 18 | console.log(stayPositive([-5, 11, -40, 30.3, -2])); // [11, 30.3] 19 | console.log(stayPositive([-11, -30])); // [] -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/strings-to-lengths.js: -------------------------------------------------------------------------------- 1 | // Write a function `stringsToLengths` that accepts an array of strings as an argument. The function 2 | // should return a new array containing the lengths of the elements of the original array. 3 | 4 | let stringsToLengths = function (words) { 5 | let lengths = []; 6 | 7 | for (let i = 0; i < words.length; i++) { 8 | lengths.push(words[i].length); 9 | } 10 | 11 | return lengths; 12 | }; 13 | 14 | console.log(stringsToLengths(["belly", "echo", "irony", "pickled"])); 15 | // [5, 4, 5, 7] 16 | 17 | console.log(stringsToLengths(["on", "off", "handmade"])); 18 | // [2, 3, 8] -------------------------------------------------------------------------------- /04_arrays/solutions/A_arrays_solution/total.js: -------------------------------------------------------------------------------- 1 | // Write a function `total` that accepts an array of numbers as an argument. The function should return 2 | // the total sum of all elements of the array. 3 | 4 | let total = function (numbers) { 5 | let sum = 0; 6 | for (let i = 0; i < numbers.length; i++) { 7 | sum += numbers[i]; 8 | } 9 | return sum; 10 | }; 11 | 12 | console.log(total([3, 2, 8])); // 13 13 | console.log(total([-5, 7, 4, 6])); // 12 14 | console.log(total([7])); // 7 15 | console.log(total([])); // 0 -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/choose-divisibles.js: -------------------------------------------------------------------------------- 1 | // Write a function `chooseDivisibles(numbers, target)` that accepts an array of numbers and a 2 | // target number as arguments. The function should return an array containing elements of the original 3 | // array that are divisible by the target. 4 | 5 | let chooseDivisibles = function (numbers, target) { 6 | let divisibles = []; 7 | 8 | for (let i = 0; i < numbers.length; i++) { 9 | let num = numbers[i]; 10 | 11 | if (num % target === 0) { 12 | divisibles.push(num); 13 | } 14 | } 15 | 16 | return divisibles; 17 | }; 18 | 19 | console.log(chooseDivisibles([40, 7, 22, 20, 24], 4)); // [40, 20, 24] 20 | console.log(chooseDivisibles([9, 33, 8, 17], 3)); // [9, 33] 21 | console.log(chooseDivisibles([4, 25, 1000], 10)); // [1000] -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/make-acronym.js: -------------------------------------------------------------------------------- 1 | // Write a function `makeAcronym` that accepts a sentence string as an argument. The function should 2 | // return a string containing the first character of each word in the sentence. 3 | 4 | let makeAcronym = function (sentence) { 5 | let words = sentence.split(' '); 6 | let acronym = ''; 7 | 8 | for (let i = 0; i < words.length; i++) { 9 | let word = words[i]; 10 | acronym += word[0]; 11 | } 12 | 13 | return acronym.toUpperCase(); 14 | }; 15 | 16 | console.log(makeAcronym("New York")); // NY 17 | console.log(makeAcronym("same stuff different day")); // SSDD 18 | console.log(makeAcronym("Laugh out loud")); // LOL 19 | console.log(makeAcronym("don't over think stuff")); // DOTS -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/maximum.js: -------------------------------------------------------------------------------- 1 | // Write a function `maximum` that accepts an array of numbers as an argument. The function should 2 | // return the largest number of the array. If the array is empty, then the function should return null. 3 | 4 | let maximum = function (numbers) { 5 | if (numbers.length === 0) { 6 | return null; 7 | } 8 | 9 | let currentMax = numbers[0]; 10 | for (let i = 1; i < numbers.length; i++) { 11 | if (numbers[i] > currentMax) { 12 | currentMax = numbers[i]; 13 | } 14 | } 15 | 16 | return currentMax; 17 | }; 18 | 19 | console.log(maximum([5, 6, 3, 7])); // 7 20 | console.log(maximum([17, 15, 19, 11, 2])); // 19 21 | console.log(maximum([])); // null -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/reverse-array.js: -------------------------------------------------------------------------------- 1 | // Write a function `reverseArray` that accepts an array as an argument. The function should return a 2 | // array containing the elements of the original array in reverse order. 3 | 4 | let reverseArray1 = function (array) { 5 | let reversed = []; 6 | 7 | for (let i = array.length - 1; i >= 0; i--) { 8 | reversed.push(array[i]); 9 | } 10 | 11 | return reversed; 12 | }; 13 | 14 | console.log(reverseArray1(["zero", "one", "two", "three"])); // ['three', 'two', 'one', 'zero'] 15 | console.log(reverseArray1([7, 1, 8])); // [8, 1, 7] 16 | 17 | 18 | let reverseArray2 = function (array) { 19 | let reversed = []; 20 | 21 | for (let i = 0; i < array.length; i++) { 22 | reversed.unshift(array[i]); 23 | } 24 | 25 | return reversed; 26 | }; 27 | 28 | console.log(reverseArray2(["zero", "one", "two", "three"])); // ['three', 'two', 'one', 'zero'] 29 | console.log(reverseArray2([7, 1, 8])); // [8, 1, 7] -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/word-count.js: -------------------------------------------------------------------------------- 1 | // Write a function `wordCount(sentence, targetWords)` that accepts a sentence string and an array of 2 | // `targetWords`. The function should return a count of the number of words of the sentence that are 3 | // in `targetWords`. 4 | 5 | let wordCount = function (sentence, targetWords) { 6 | let words = sentence.split(' '); 7 | let count = 0; 8 | 9 | for (let i = 0; i < words.length; i++) { 10 | let word = words[i]; 11 | 12 | if (targetWords.includes(word)) { 13 | count++; 14 | } 15 | } 16 | 17 | return count; 18 | }; 19 | 20 | console.log(wordCount("open the window please", ["please", "open", "sorry"])); // 2 21 | console.log(wordCount("drive to the cinema", ["the", "driver"])); // 1 22 | console.log(wordCount("can I have that can", ["can", "I"])); // 3 -------------------------------------------------------------------------------- /04_arrays/solutions/B_arrays_solution/your-average-function.js: -------------------------------------------------------------------------------- 1 | // Write a function `yourAverageFunction` that accepts an array of numbers as an argument. The 2 | // function should return the average of all elements of the array. If the input array is empty, 3 | // then the function should return null. 4 | 5 | let yourAverageFunction = function (nums) { 6 | if (nums.length === 0) { 7 | return null; 8 | } 9 | 10 | let sum = 0; 11 | for (let i = 0; i < nums.length; i++) { 12 | sum += nums[i]; 13 | } 14 | 15 | return sum / nums.length; 16 | }; 17 | 18 | console.log(yourAverageFunction([5, 2, 7, 24])); // 9.5 19 | console.log(yourAverageFunction([100, 6])); // 53 20 | console.log(yourAverageFunction([31, 32, 40, 12, 33])); // 29.6 21 | console.log(yourAverageFunction([])); // null -------------------------------------------------------------------------------- /04_arrays/solutions/C_arrays_solution/alternating-caps.js: -------------------------------------------------------------------------------- 1 | // Write a function `alternatingCaps` that accepts a sentence string as an argument. The function should 2 | // return the sentence where words alternate between lowercase and uppercase. 3 | 4 | let alternatingCaps = function (sentence) { 5 | let words = sentence.split(' '); 6 | let newWords = []; 7 | 8 | for (let i = 0; i < words.length; i++) { 9 | let word = words[i]; 10 | if (i % 2 === 0) { 11 | newWords.push(word.toLowerCase()); 12 | } else { 13 | newWords.push(word.toUpperCase()); 14 | } 15 | } 16 | 17 | return newWords.join(' '); 18 | }; 19 | 20 | console.log(alternatingCaps("take them to school")); // 'take THEM to SCHOOL' 21 | console.log(alternatingCaps("What did ThEy EAT before?")); // 'what DID they EAT before?' -------------------------------------------------------------------------------- /04_arrays/solutions/C_arrays_solution/common-elements.js: -------------------------------------------------------------------------------- 1 | // Write a function `commonElements` that accepts two arrays as arguments. The function should return 2 | // a new array containing the elements that are found in both of the input arrays. The order of 3 | // the elements in the output array doesn't matter as long as the function returns the correct elements. 4 | 5 | let commonElements = function (array1, array2) { 6 | let common = []; 7 | 8 | for (let i = 0; i < array1.length; i++) { 9 | let ele = array1[i]; 10 | 11 | if (array2.includes(ele)) { 12 | common.push(ele); 13 | } 14 | } 15 | 16 | return common; 17 | }; 18 | 19 | let arr1 = ["a", "c", "d", "b"]; 20 | let arr2 = ["b", "a", "y"]; 21 | console.log(commonElements(arr1, arr2)); // ['a', 'b'] 22 | 23 | let arr3 = [4, 7]; 24 | let arr4 = [32, 7, 1, 4]; 25 | console.log(commonElements(arr3, arr4)); // [4, 7] -------------------------------------------------------------------------------- /04_arrays/solutions/C_arrays_solution/lengthiest-word.js: -------------------------------------------------------------------------------- 1 | // Write a function `lengthiestWord` that accepts a sentence string as an argument. The function should 2 | // return the longest word of the sentence. If there is a tie, return the word that appears later 3 | // in the sentence. 4 | 5 | let lengthiestWord = function(sentence) { 6 | let words = sentence.split(' '); 7 | let longest = words[0]; // pretty 8 | 9 | for (let i = 1; i < words.length; i++) { 10 | let word = words[i]; 11 | if (word.length >= longest.length) { 12 | longest = word; 13 | } 14 | } 15 | 16 | return longest; 17 | }; 18 | 19 | 20 | console.log(lengthiestWord("I am pretty hungry")); // 'hungry' 21 | console.log(lengthiestWord("we should think outside of the box")); // 'outside' 22 | console.log(lengthiestWord("down the rabbit hole")); // 'rabbit' 23 | console.log(lengthiestWord("simmer down")); // 'simmer' -------------------------------------------------------------------------------- /04_arrays/solutions/C_arrays_solution/number-range.js: -------------------------------------------------------------------------------- 1 | // Write a function `numberRange(min, max, step)` that accepts three numbers as arguments, `min`, 2 | // `max`, and `step`. The function should return all numbers between `min` and `max` at `step` intervals. 3 | // `min` and `max` are inclusive. 4 | 5 | let numberRange = function (min, max, step) { 6 | let range = []; 7 | 8 | for (let i = min; i <= max; i += step) { 9 | range.push(i); 10 | } 11 | 12 | return range; 13 | }; 14 | 15 | console.log(numberRange(10, 40, 5)); // [10, 15, 20, 25, 30, 35, 40] 16 | console.log(numberRange(14, 24, 3)); // [14, 17, 20, 23] 17 | console.log(numberRange(8, 35, 6)); // [8, 14, 20, 26, 32] -------------------------------------------------------------------------------- /04_arrays/solutions/C_arrays_solution/remove-short-words.js: -------------------------------------------------------------------------------- 1 | // Write a function `removeShortWords` that accepts a sentence string as an argument. The function 2 | // should return a new sentence where all of the words shorter than 4 characters are removed. 3 | 4 | let removeShortWords = function(sentence) { 5 | let words = sentence.split(' '); 6 | let chosenWords = []; 7 | 8 | for (let i = 0; i < words.length; i++) { 9 | let word = words[i]; 10 | 11 | if (word.length >= 4) { 12 | chosenWords.push(word); 13 | } 14 | } 15 | 16 | return chosenWords.join(' '); 17 | }; 18 | 19 | console.log(removeShortWords("knock on the door will you")); // 'knock door will' 20 | console.log(removeShortWords("a terrible plan")); // 'terrible plan' 21 | console.log(removeShortWords("run faster that way")); // 'faster that' -------------------------------------------------------------------------------- /05_nesting/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/05_nesting/.DS_Store -------------------------------------------------------------------------------- /05_nesting/exercises/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/05_nesting/exercises/.DS_Store -------------------------------------------------------------------------------- /05_nesting/exercises/A_nested_loops_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Loop the Loop 2 | 3 | _Nested Loops_ are when we write a loop within the body of another loop. The pattern can be useful 4 | to generate "pairs" of things. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | + [Lecture](https://youtu.be/68VmjvtOGGI) 9 | 10 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 11 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 12 | 13 | + [Walkthrough](https://youtu.be/seiovGIFokM) 14 | 15 | If you are comfortable with single loops, then you'll find nested loops pretty intuitive. Good thing 16 | we practiced single loops a ton already. 17 | 18 | -AZ 19 | 20 | ## 0. Reading Nested Loops. 21 | 22 | Create a new folder named `A_nested_loops`, this is where you'll save all of your files. Predict 23 | what would print when executing each of the snippets below. Test your prediction by typing the 24 | snippet into their own files and executing the program. Don't copy and paste. 25 | 26 | Write the below code into a file named `snippet-1.js`. Make a prediction for what it will print. Run 27 | it. 28 | 29 | ```js 30 | // snippet 1 31 | for (let i = 1; i <= 4; i++) { 32 | for (let j = 1; j <= 3; j++) { 33 | console.log(i, j); 34 | } 35 | } 36 | ``` 37 | 38 | Write the below code into a file named `snippet-2.js`. Make a prediction for what it will print. Run 39 | it. 40 | 41 | ```js 42 | // snippet 2 43 | for (let n = 0; n < 2; n++) { 44 | console.log("n=" + n); 45 | for (let m = 0; m < 5; m++) { 46 | console.log(" m=" + m); 47 | } 48 | console.log("n=" + n); 49 | } 50 | ``` 51 | 52 | Write the below code into a file named `snippet-3.js`. Make a prediction for what it will print. Run 53 | it. 54 | 55 | ```js 56 | // snippet 3 57 | let friends = ["philip", "abby", "phelipe", "simcha"]; 58 | 59 | for (let i = 0; i < friends.length; i++) { 60 | for (let j = 0; j < friends.length; j++) { 61 | console.log(friends[i], friends[j]); 62 | } 63 | } 64 | ``` 65 | 66 | Write the below code into a file named `snippet-4.js`. Make a prediction for what it will print. Run 67 | it. 68 | 69 | ```js 70 | // snippet 4 71 | let locations = ["flatbush", "williamsburg", "bushwick", "greenpoint"]; 72 | 73 | for (let i = 0; i < locations.length; i++) { 74 | for (let j = i + 1; j < locations.length; j++) { 75 | console.log(locations[i], locations[j]); 76 | } 77 | } 78 | ``` 79 | 80 | Write the below code into a file named `snippet-5.js`. Make a prediction for what it will print. Run 81 | it. 82 | 83 | ```js 84 | // snippet 5 85 | let colors = ["red", "purple", "orange"]; 86 | 87 | for (let i = 0; i < colors.length; i++) { 88 | let colorStr = colors[i]; 89 | console.log(colorStr); 90 | 91 | for (let j = 0; j < colorStr.length; j++) { 92 | let char = colorStr[j]; 93 | console.log(char); 94 | } 95 | } 96 | ``` 97 | 98 | ## 1. Writing Nested Loops. 99 | 100 | In this section of the exercise you'll be prompted to write a function to accomplish a specific 101 | task. Read the description and look at the example output to figure out what should be implemented 102 | within the function. Write each function to a separate file in the `A_nested_loops_exercise` folder 103 | and test your work by running the file. 104 | 105 | ### pairPrint 106 | 107 | Create a `pair-print.js` file and solve the function below: 108 | 109 | ```js 110 | // Write a function `pairPrint` that accepts an array as an argument. The function should print 111 | // all unique pairs of elements in the array. The function doesn't need to return any value. It 112 | // should just print to the terminal. 113 | 114 | pairPrint(["artichoke", "broccoli", "carrot", "daikon"]); 115 | // prints 116 | // artichoke - broccoli 117 | // artichoke - carrot 118 | // artichoke - daikon 119 | // broccoli - carrot 120 | // broccoli - daikon 121 | // carrot - daikon 122 | 123 | pairPrint(["apple", "banana", "clementine"]); 124 | // prints 125 | // apple - banana 126 | // apple - clementine 127 | // banana - clementine 128 | ``` 129 | 130 | ### printCombinations 131 | 132 | Create a `print-combinations.js` file and solve the function below: 133 | 134 | ```js 135 | // Write a function `printCombinations`that accepts two arrays as arguments. The function should 136 | // print all combinations of the elements generated by taking an element from the first array and 137 | // and an element from the second array. The function doesn't need to return any value. It 138 | // should just print to the terminal. 139 | 140 | let colors = ["gray", "cream", "cyan"]; 141 | let clothes = ["shirt", "flannel"]; 142 | printCombinations(colors, clothes); 143 | // prints 144 | // gray shirt 145 | // gray flannel 146 | // cream shirt 147 | // cream flannel 148 | // cyan shirt 149 | // cyan flannel 150 | 151 | printCombinations(["hot", "cold"], ["soup", "tea"]); 152 | // prints 153 | // hot soup 154 | // hot tea 155 | // cold soup 156 | // cold tea 157 | ``` 158 | 159 | ### twoSum 160 | 161 | Create a `two-sum.js` file and solve the function below: 162 | 163 | ```js 164 | // Write a function `twoSum(numbers, target)` that accepts an array of numbers and a target number 165 | // as an argument. The function should return a boolean indicating whether or not there exists a pair 166 | // of distinct elements in the array that sum to the target. 167 | 168 | console.log(twoSum([2, 3, 5, 9], 7)); // true 169 | console.log(twoSum([2, 3, 5, 9], 4)); // false 170 | console.log(twoSum([6, 3, 4], 10)); // true 171 | console.log(twoSum([6, 5, 1], 10)); // false 172 | ``` 173 | 174 | ## Walkthrough 175 | 176 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 177 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 178 | Watch it in 1.5x speed if you like. 179 | -------------------------------------------------------------------------------- /05_nesting/exercises/B_two_dimensional_arrays_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Adding a New Dimension 2 | 3 | A _two-dimensional array_ is an array that contains other arrays as elements. Programmers use 4 | multidimensional arrays when dealing with data that has many layers. It is also useful to represent 5 | a grid space. 6 | 7 | Before you start this exercise, make sure you have watched the lecture first. 8 | 9 | + [Lecture](https://youtu.be/CDpJ4PIWAlE) 10 | 11 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 12 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 13 | 14 | + [Walkthrough](https://youtu.be/ajSXu2D2gzg) 15 | 16 | Loops in loops? Arrays in arrays? What's next, functions in functions? 17 | 18 | -AZ 19 | 20 | ## Instructions 21 | 22 | Create a new folder named `B_two_dimensional_arrays_exercise`, this is where you'll save all of your 23 | files. Solve each of the prompts below. Create a new file for each prompt and execute your code to 24 | see if it matches the example output. 25 | 26 | Heads up. Not all of the problems below require 2D arrays. It's up to you to figure out what is the 27 | right tool for the job. 28 | 29 | ### print2d 30 | 31 | Create a `print-2d.js` file and solve the function below: 32 | 33 | ```js 34 | // Write a function `print2d` that accepts a two-dimensional array as an argument. The function 35 | // should print all inner elements of the array. 36 | 37 | let array1 = [ 38 | ["a", "b", "c", "d"], 39 | ["e", "f"], 40 | ["g", "h", "i"], 41 | ]; 42 | print2d(array1); 43 | // prints 44 | // a 45 | // b 46 | // c 47 | // d 48 | // e 49 | // f 50 | // g 51 | // h 52 | // i 53 | 54 | let array2 = [[9, 3, 4], [11], [42, 100]]; 55 | print2d(array2); 56 | // prints 57 | // 9 58 | // 3 59 | // 4 60 | // 11 61 | // 42 62 | // 100 63 | ``` 64 | 65 | ### makeMatrix 66 | 67 | Create a `make-matrix.js` file and solve the function below: 68 | 69 | ```js 70 | // Write a function `makeMatrix(m, n, value)` that accepts three arguments. The function should return 71 | // a 2-dimensional array of height `m` and width `n` that contains the `value` as every element. 72 | 73 | console.log(makeMatrix(3, 5, null)); 74 | // [ 75 | // [ null, null, null, null, null ], 76 | // [ null, null, null, null, null ], 77 | // [ null, null, null, null, null ] 78 | // ] 79 | 80 | console.log(makeMatrix(4, 2, "x")); 81 | // [ 82 | // [ 'x', 'x' ], 83 | // [ 'x', 'x' ], 84 | // [ 'x', 'x' ], 85 | // [ 'x', 'x' ] 86 | // ] 87 | 88 | console.log(makeMatrix(2, 2, 0)); 89 | // [ 90 | // [ 0, 0 ], 91 | // [ 0, 0 ] 92 | // ] 93 | ``` 94 | 95 | ### totalProduct 96 | 97 | Create a `total-product.js` file and solve the function below: 98 | 99 | ```js 100 | // Write a function `totalProduct(array)` that accepts a 2D array of numbers. The function should return 101 | // the total product of all numbers in the array. 102 | 103 | let array1 = [ 104 | [3, 5, 2], 105 | [6, 2], 106 | ]; 107 | console.log(totalProduct(array1)); // 360 108 | 109 | let array2 = [ 110 | [4, 6], 111 | [2, 3], 112 | [1, 2], 113 | ]; 114 | console.log(totalProduct(array2)); // 288 115 | ``` 116 | 117 | ### twoSumPairs 118 | 119 | Create a `two-sum-pairs.js` file and solve the function below: 120 | 121 | ```js 122 | // Write a function `twoSumPairs(numbers, target)` that accepts an array of numbers and a target number 123 | // as arguments. The function should return a 2D array containing all unique pairs of elements that 124 | // sum to the target. 125 | 126 | console.log(twoSumPairs([2, 3, 4, 6, 5], 8)); // [ [2, 6], [3, 5] ] 127 | console.log(twoSumPairs([10, 7, 4, 5, 2], 12)); // [ [10, 2], [7, 5] ] 128 | console.log(twoSumPairs([3, 9, 8], 11)); // [ [3, 8] ] 129 | console.log(twoSumPairs([3, 9, 8], 10)); // [ ] 130 | ``` 131 | 132 | ### zipper 133 | 134 | Create a `zipper.js` file and solve the function below: 135 | 136 | ```js 137 | // Write a function `zipper` that accepts two arrays as arguments. The function should return a 2D 138 | // array containing pairs of elements at the same indices. You can assume that the arrays have the 139 | // same length. 140 | 141 | let array1 = ["a", "b", "c", "d"]; 142 | let array2 = [-1, -2, -3, -4]; 143 | console.log(zipper(array1, array2)); 144 | // [ 145 | // ['a', -1], 146 | // ['b', -2], 147 | // ['c', -3], 148 | // ['d', -4], 149 | // ] 150 | 151 | let array3 = ["whisper", "talk", "shout"]; 152 | let array4 = ["quiet", "normal", "loud"]; 153 | console.log(zipper(array3, array4)); 154 | // [ 155 | // ['whisper', 'quiet'], 156 | // ['talk', 'normal'], 157 | // ['shout', 'loud'], 158 | // ] 159 | ``` 160 | 161 | ## Walkthrough 162 | 163 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 164 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 165 | Watch it in 1.5x speed if you like. 166 | -------------------------------------------------------------------------------- /05_nesting/exercises/C_more_problems_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Problems Make Perfect 2 | 3 | More problems `===` more practice. 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | I'll keep writing problems if you keep coding. Deal? 9 | 10 | -AZ 11 | 12 | ## Instructions 13 | 14 | Create a new folder named `C_more_problems`, this is where you'll save all of your files. Solve each 15 | of the prompts below. Create a new file for each prompt and execute your code to see if it matches 16 | the example output. 17 | 18 | This problem set tests a few different concepts. Any concepts we've covered are fair game. Some of 19 | the following problems are tough, but they only require knowledge of concepts we've already covered. 20 | Give them a shot. 21 | 22 | ### removeDupes 23 | 24 | Create a `remove-dupes.js` file and solve the function below: 25 | 26 | ```js 27 | // Write a function `removeDupes` that accepts an array as an argument. The function should return a 28 | // new array where each element only appears once. 29 | 30 | console.log(removeDupes(["x", "y", "y", "x", "z"])); // ['x', 'y', 'z'] 31 | console.log(removeDupes([false, false, true, false])); // [false, true] 32 | console.log(removeDupes([42, 5, 7, 42, 7, 3, 7, 7])); // [42, 5, 7, 3] 33 | ``` 34 | 35 | ### removeVowels 36 | 37 | Create a `remove-vowels.js` file and solve the function below: 38 | 39 | ```js 40 | // Write a function `removeVowels` that accepts a string as an argument. The function should return 41 | // a version of the string where all vowels are removed. 42 | 43 | console.log(removeVowels("jello")); // jll 44 | console.log(removeVowels("sensitivity")); // snstvty 45 | console.log(removeVowels("cellar door")); // cllr dr 46 | ``` 47 | 48 | ### spam 49 | 50 | Create a `spam.js` file and solve the function below: 51 | 52 | ```js 53 | // Write a function `spam` that accepts a 2D array as an argument. The array contains pairs as elements. 54 | // The first element of every pair is a number and the second element is a word. The function should 55 | // return a string containing the words repeated the specified number of times. See the examples. 56 | 57 | let array1 = [ 58 | ["hi", 3], 59 | ["bye", 2], 60 | ]; 61 | console.log(spam(array1)); // 'hi hi hi bye bye' 62 | 63 | let array2 = [ 64 | ["cat", 1], 65 | ["dog", 2], 66 | ["bird", 4], 67 | ]; 68 | console.log(spam(array2)); // 'cat dog dog bird bird bird bird' 69 | ``` 70 | 71 | ### removeFirstVowel 72 | 73 | Create a `remove-first-vowel.js` file and solve the function below: 74 | 75 | ```js 76 | // Write a function `removeFirstVowel` that accepts a string as an argument. The function should return 77 | // the string with it's first vowel removed. 78 | 79 | console.log(removeFirstVowel("volcano")); // 'vlcano' 80 | console.log(removeFirstVowel("celery")); // 'clery' 81 | console.log(removeFirstVowel("juice")); // 'jice' 82 | ``` 83 | 84 | ### shortenLongWords 85 | 86 | Create a `shorten-long-words.js` file and solve the function below: 87 | 88 | ```js 89 | // Write a function `shortenLongWords` that accepts a sentence string as an argument. The function 90 | // should return the same sentence where words longer than 4 characters have their vowels removed. 91 | 92 | console.log(shortenLongWords("they are very noble people")); // 'they are very nbl ppl' 93 | console.log(shortenLongWords("stick with it")); // 'stck with it' 94 | console.log(shortenLongWords("ballerina, you must have seen her")); // 'bllrna, you must have seen her' 95 | ``` 96 | -------------------------------------------------------------------------------- /05_nesting/solutions/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hey-Programmers/learn-javascript/79f6e657346f4b2e3d07766255b1adb7b37e0bd1/05_nesting/solutions/.DS_Store -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/pair-print.js: -------------------------------------------------------------------------------- 1 | // Write a function `pairPrint` that accepts an array as an argument. The function should print 2 | // all unique pairs of elements in the array. The function doesn't need to return any value. It 3 | // should just print to the terminal. 4 | 5 | let pairPrint = function (array) { 6 | for (let i = 0; i < array.length; i++) { 7 | for (let j = i + 1; j < array.length; j++) { 8 | console.log(array[i] + ' - ' + array[j]); 9 | } 10 | } 11 | }; 12 | 13 | pairPrint(["artichoke", "broccoli", "carrot", "daikon"]); 14 | // prints 15 | // artichoke - broccoli 16 | // artichoke - carrot 17 | // artichoke - daikon 18 | // broccoli - carrot 19 | // broccoli - daikon 20 | // carrot - daikon 21 | 22 | pairPrint(["apple", "banana", "clementine"]); 23 | // prints 24 | // apple - banana 25 | // apple - clementine 26 | // banana - clementine 27 | -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/print-combinations.js: -------------------------------------------------------------------------------- 1 | // Write a function `printCombinations`that accepts two arrays as arguments. The function should 2 | // print all combinations of the elements generated by taking an element from the first array and 3 | // and an element from the second array. The function doesn't need to return any value. It 4 | // should just print to the terminal. 5 | 6 | 7 | let printCombinations = function (array1, array2) { 8 | for (let i = 0; i < array1.length; i++) { 9 | let ele1 = array1[i]; 10 | 11 | for (let j = 0; j < array2.length; j++) { 12 | let ele2 = array2[j]; 13 | console.log(ele1, ele2); 14 | } 15 | } 16 | }; 17 | 18 | let colors = ["gray", "cream", "cyan"]; 19 | let clothes = ["shirt", "flannel"]; 20 | printCombinations(colors, clothes); 21 | // prints 22 | // gray shirt 23 | // gray flannel 24 | // cream shirt 25 | // cream flannel 26 | // cyan shirt 27 | // cyan flannel 28 | 29 | printCombinations(["hot", "cold"], ["soup", "tea"]); 30 | // prints 31 | // hot soup 32 | // hot tea 33 | // cold soup 34 | // cold tea -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/snippet-1.js: -------------------------------------------------------------------------------- 1 | // snippet 1 2 | for (let i = 1; i <= 4; i++) { 3 | for (let j = 1; j <= 3; j++) { 4 | console.log(i, j); 5 | } 6 | } 7 | 8 | // 1 1 9 | // 1 2 10 | // 1 3 11 | // 2 1 12 | // 2 2 13 | // 2 3 14 | // 3 1 15 | // 3 2 16 | // 3 3 17 | // 4 1 18 | // 4 2 19 | // 4 3 -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/snippet-2.js: -------------------------------------------------------------------------------- 1 | // snippet 2 2 | for (let n = 0; n < 2; n++) { 3 | console.log("n=" + n); 4 | for (let m = 0; m < 5; m++) { 5 | console.log(" m=" + m); 6 | } 7 | console.log("n=" + n); 8 | } 9 | 10 | // n=0 11 | // m=0 12 | // m=1 13 | // m=2 14 | // m=3 15 | // m=4 16 | // n=0 17 | // n=1 18 | // m=0 19 | // m=1 20 | // m=2 21 | // m=3 22 | // m=4 23 | // n=1 -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/snippet-3.js: -------------------------------------------------------------------------------- 1 | // snippet 3 2 | let friends = ["philip", "abby", "phelipe", "simcha"]; 3 | 4 | for (let i = 0; i < friends.length; i++) { 5 | for (let j = 0; j < friends.length; j++) { 6 | console.log(friends[i], friends[j]); 7 | } 8 | } 9 | 10 | // philip philip 11 | // philip abby 12 | // philip phelipe 13 | // philip simcha 14 | // abby philip 15 | // abby abby 16 | // abby phelipe 17 | // abby simcha 18 | // phelipe philip 19 | // phelipe abby 20 | // phelipe phelipe 21 | // phelipe simcha 22 | // simcha philip 23 | // simcha abby 24 | // simcha phelipe 25 | // simcha simcha -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/snippet-4.js: -------------------------------------------------------------------------------- 1 | // snippet 4 2 | let locations = ["flatbush", "williamsburg", "bushwick", "greenpoint"]; 3 | 4 | for (let i = 0; i < locations.length; i++) { 5 | for (let j = i + 1; j < locations.length; j++) { 6 | console.log(locations[i], locations[j]); 7 | } 8 | } 9 | 10 | // flatbush williamsburg 11 | // flatbush bushwick 12 | // flatbush greenpoint 13 | // williamsburg bushwick 14 | // williamsburg greenpoint 15 | // bushwick greenpoint -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/snippet-5.js: -------------------------------------------------------------------------------- 1 | // snippet 5 2 | let colors = ["red", "purple", "orange"]; 3 | 4 | for (let i = 0; i < colors.length; i++) { 5 | let colorStr = colors[i]; 6 | console.log(colorStr); 7 | 8 | for (let j = 0; j < colorStr.length; j++) { 9 | let char = colorStr[j]; 10 | console.log(char); 11 | } 12 | } 13 | 14 | // red 15 | // r 16 | // e 17 | // d 18 | // purple 19 | // p 20 | // u 21 | // r 22 | // p 23 | // l 24 | // e 25 | // orange 26 | // o 27 | // r 28 | // a 29 | // n 30 | // g 31 | // e -------------------------------------------------------------------------------- /05_nesting/solutions/A_nested_loops_solution/two-sum.js: -------------------------------------------------------------------------------- 1 | // Write a function `twoSum(numbers, target)` that accepts an array of numbers and a target number 2 | // as an argument. The function should return a boolean indicating whether or not there exists a pair 3 | // of distinct elements in the array that sum to the target. 4 | 5 | let twoSum = function (numbers, target) { 6 | for (let i = 0; i < numbers.length; i++) { 7 | for (let j = i + 1; j < numbers.length; j++) { 8 | if (numbers[i] + numbers[j] === target) { 9 | return true; 10 | } 11 | } 12 | } 13 | 14 | return false; 15 | }; 16 | 17 | console.log(twoSum([2, 3, 5, 9], 7)); // true 18 | console.log(twoSum([2, 3, 5, 9], 4)); // false 19 | console.log(twoSum([6, 3, 4], 10)); // true 20 | console.log(twoSum([6, 5, 1], 10)); // false -------------------------------------------------------------------------------- /05_nesting/solutions/B_two_dimensional_arrays_solution/make-matrix.js: -------------------------------------------------------------------------------- 1 | // Write a function `makeMatrix(m, n, value)` that accepts three arguments. The function should return 2 | // a 2-dimensional array of height `m` and width `n` that contains the `value` as every element. 3 | 4 | let makeMatrix = function (m, n, value) { 5 | let matrix = []; 6 | 7 | for (let i = 0; i < m; i++) { 8 | let row = []; 9 | 10 | for (let j = 0; j < n; j++) { 11 | row.push(value); 12 | } 13 | 14 | matrix.push(row); 15 | } 16 | 17 | 18 | return matrix; 19 | }; 20 | 21 | console.log(makeMatrix(3, 5, null)); 22 | // [ 23 | // [ null, null, null, null, null ], 24 | // [ null, null, null, null, null ], 25 | // [ null, null, null, null, null ] 26 | // ] 27 | 28 | console.log(makeMatrix(4, 2, "x")); 29 | // [ 30 | // [ 'x', 'x' ], 31 | // [ 'x', 'x' ], 32 | // [ 'x', 'x' ], 33 | // [ 'x', 'x' ] 34 | // ] 35 | 36 | console.log(makeMatrix(2, 2, 0)); 37 | // [ 38 | // [ 0, 0 ], 39 | // [ 0, 0 ] 40 | // ] -------------------------------------------------------------------------------- /05_nesting/solutions/B_two_dimensional_arrays_solution/print-2d.js: -------------------------------------------------------------------------------- 1 | // Write a function `print2d` that accepts a two-dimensional array as an argument. The function 2 | // should print all inner elements of the array. 3 | 4 | let print2d = function (array) { 5 | for (let i = 0; i < array.length; i++) { 6 | let subarray = array[i]; 7 | for (let j = 0; j < subarray.length; j++) { 8 | console.log(subarray[j]); 9 | } 10 | } 11 | }; 12 | 13 | 14 | let array1 = [ 15 | ["a", "b", "c", "d"], 16 | ["e", "f"], 17 | ["g", "h", "i"], 18 | ]; 19 | print2d(array1); 20 | // prints 21 | // a 22 | // b 23 | // c 24 | // d 25 | // e 26 | // f 27 | // g 28 | // h 29 | // i 30 | 31 | let array2 = [[9, 3, 4], [11], [42, 100]]; 32 | print2d(array2); 33 | // prints 34 | // 9 35 | // 3 36 | // 4 37 | // 11 38 | // 42 39 | // 100 -------------------------------------------------------------------------------- /05_nesting/solutions/B_two_dimensional_arrays_solution/total-product.js: -------------------------------------------------------------------------------- 1 | // Write a function `totalProduct(array)` that accepts a 2D array of numbers. The function should return 2 | // the total product of all numbers in the array. 3 | 4 | let totalProduct = function (array) { 5 | let product = 1; 6 | 7 | for (let i = 0; i < array.length; i++) { 8 | let subarray = array[i]; 9 | 10 | for (let j = 0; j < subarray.length; j++) { 11 | product *= subarray[j]; 12 | } 13 | } 14 | 15 | return product; 16 | }; 17 | 18 | let array1 = [ 19 | [3, 5, 2], 20 | [6, 2], 21 | ]; 22 | console.log(totalProduct(array1)); // 360 23 | 24 | let array2 = [ 25 | [4, 6], 26 | [2, 3], 27 | [1, 2], 28 | ]; 29 | console.log(totalProduct(array2)); // 288 -------------------------------------------------------------------------------- /05_nesting/solutions/B_two_dimensional_arrays_solution/two-sum-pairs.js: -------------------------------------------------------------------------------- 1 | // Write a function `twoSumPairs(numbers, target)` that accepts an array of numbers and a target number 2 | // as arguments. The function should return a 2D array containing all unique pairs of elements that 3 | // sum to the target. 4 | 5 | let twoSumPairs = function (numbers, target) { 6 | let pairs = []; 7 | 8 | for (let i = 0; i < numbers.length; i++) { 9 | let num1 = numbers[i]; 10 | 11 | for (let j = i + 1; j < numbers.length; j++) { 12 | let num2 = numbers[j]; 13 | 14 | if (num1 + num2 === target) { 15 | let pair = [ num1, num2 ]; 16 | pairs.push(pair); 17 | } 18 | } 19 | } 20 | 21 | return pairs; 22 | }; 23 | 24 | console.log(twoSumPairs([2, 3, 4, 6, 5], 8)); // [ [2, 6], [3, 5] ] 25 | console.log(twoSumPairs([10, 7, 4, 5, 2], 12)); // [ [10, 2], [7, 5] ] 26 | console.log(twoSumPairs([3, 9, 8], 11)); // [ [3, 8] ] 27 | console.log(twoSumPairs([3, 9, 8], 10)); // [ ] -------------------------------------------------------------------------------- /05_nesting/solutions/B_two_dimensional_arrays_solution/zipper.js: -------------------------------------------------------------------------------- 1 | // Write a function `zipper` that accepts two arrays as arguments. The function should return a 2D 2 | // array containing pairs of elements at the same indices. 3 | 4 | 5 | let zipper = function (arr1, arr2) { 6 | let result = []; 7 | 8 | for (let i = 0; i < arr1.length; i++) { 9 | let el1 = arr1[i]; 10 | let el2 = arr2[i]; 11 | let pair = [ el1, el2 ]; 12 | result.push(pair); 13 | } 14 | 15 | return result; 16 | }; 17 | 18 | let array1 = ["a", "b", "c", "d"]; 19 | let array2 = [-1, -2, -3, -4]; 20 | console.log(zipper(array1, array2)); 21 | // [ 22 | // ['a', -1], 23 | // ['b', -2], 24 | // ['c', -3], 25 | // ['d', -4], 26 | // ] 27 | 28 | let array3 = ["whisper", "talk", "shout"]; 29 | let array4 = ["quiet", "normal", "loud"]; 30 | console.log(zipper(array3, array4)); 31 | // [ 32 | // ['whisper', 'quiet'], 33 | // ['talk', 'normal'], 34 | // ['shout', 'loud'], 35 | // ] -------------------------------------------------------------------------------- /05_nesting/solutions/C_more_problems_solution/remove-dupes.js: -------------------------------------------------------------------------------- 1 | // Write a function `removeDupes` that accepts an array as an argument. The function should return a 2 | // new array where each element only appears once. 3 | 4 | let removeDupes = function (array) { 5 | let uniques = []; 6 | 7 | for (let i = 0; i < array.length; i++) { 8 | let ele = array[i]; 9 | if (!uniques.includes(ele)) { 10 | uniques.push(ele); 11 | } 12 | } 13 | 14 | return uniques; 15 | }; 16 | 17 | console.log(removeDupes(["x", "y", "y", "x", "z"])); // ['x', 'y', 'z'] 18 | console.log(removeDupes([false, false, true, false])); // [false, true] 19 | console.log(removeDupes([42, 5, 7, 42, 7, 3, 7, 7])); // [42, 5, 7, 3] -------------------------------------------------------------------------------- /05_nesting/solutions/C_more_problems_solution/remove-first-vowel.js: -------------------------------------------------------------------------------- 1 | // Write a function `removeFirstVowel` that accepts a string as an argument. The function should return 2 | // the string with it's first vowel removed. 3 | 4 | let removeFirstVowel = function (str) { 5 | let vowels = 'aeiou'; 6 | 7 | for (let i = 0; i < str.length; i++) { 8 | let char = str[i]; 9 | if (vowels.includes(char)) { 10 | return str.slice(0, i) + str.slice(i + 1); 11 | } 12 | } 13 | 14 | return str; 15 | }; 16 | 17 | console.log(removeFirstVowel("volcano")); // 'vlcano' 18 | console.log(removeFirstVowel("celery")); // 'clery' 19 | console.log(removeFirstVowel("juice")); // 'jice' 20 | console.log(removeFirstVowel("bridge")); // 'brdge' 21 | console.log(removeFirstVowel("try")); // 'try' 22 | -------------------------------------------------------------------------------- /05_nesting/solutions/C_more_problems_solution/remove-vowels.js: -------------------------------------------------------------------------------- 1 | // Write a function `removeVowels` that accepts a string as an argument. The function should return 2 | // a version of the string where all vowels are removed. 3 | 4 | let removeVowels = function (str) { 5 | let newStr = ''; 6 | let vowels = 'aeiou'; 7 | 8 | for (let i = 0; i < str.length; i++) { 9 | let char = str[i]; 10 | if (!vowels.includes(char)) { 11 | newStr += char; 12 | } 13 | } 14 | 15 | return newStr; 16 | }; 17 | 18 | console.log(removeVowels("jello")); // jll 19 | console.log(removeVowels("sensitivity")); // snstvty 20 | console.log(removeVowels("cellar door")); // cllr dr -------------------------------------------------------------------------------- /05_nesting/solutions/C_more_problems_solution/shorten-long-words.js: -------------------------------------------------------------------------------- 1 | // Write a function `shortenLongWords` that accepts a sentence string as an argument. The function 2 | // should return the same sentence where words longer than 4 characters have their vowels removed. 3 | 4 | let removeVowels = function (str) { 5 | let newStr = ''; 6 | let vowels = 'aeiou'; 7 | 8 | for (let i = 0; i < str.length; i++) { 9 | let char = str[i]; 10 | if (!vowels.includes(char)) { 11 | newStr += char; 12 | } 13 | } 14 | 15 | return newStr; 16 | }; 17 | 18 | let shortenLongWords = function (sentence) { 19 | let words = sentence.split(' '); 20 | let newWords = []; 21 | 22 | for (let i = 0; i < words.length; i++) { 23 | let word = words[i]; 24 | if (word.length > 4) { 25 | let noVowels = removeVowels(word); 26 | newWords.push(noVowels); 27 | } else { 28 | // leave it the same 29 | newWords.push(word); 30 | } 31 | } 32 | 33 | return newWords.join(' '); 34 | }; 35 | 36 | console.log(shortenLongWords("they are very noble people")); // 'they are very nbl ppl' 37 | console.log(shortenLongWords("stick with it")); // 'stck with it' 38 | console.log(shortenLongWords("ballerina, you must have seen her")); // 'bllrna, you must have seen her -------------------------------------------------------------------------------- /05_nesting/solutions/C_more_problems_solution/spam.js: -------------------------------------------------------------------------------- 1 | // Write a function `spam` that accepts a 2D array as an argument. The array contains pairs as elements. 2 | // The first element of every pair is a number and the second element is a word. The function should 3 | // return a string containing the words repeated the specified number of times. See the examples. 4 | 5 | let spam = function (pairs) { 6 | let words = []; 7 | 8 | for (let i = 0; i < pairs.length; i++) { 9 | let pair = pairs[i]; 10 | let word = pair[0]; 11 | let num = pair[1]; 12 | 13 | for (let j = 0; j < num; j++) { 14 | words.push(word); 15 | } 16 | } 17 | 18 | return words.join(' '); 19 | }; 20 | 21 | let array1 = [ 22 | ["hi", 3], 23 | ["bye", 2], 24 | ]; 25 | console.log(spam(array1)); // 'hi hi hi bye bye' 26 | 27 | let array2 = [ 28 | ["cat", 1], 29 | ["dog", 2], 30 | ["bird", 4], 31 | ]; 32 | console.log(spam(array2)); // 'cat dog dog bird bird bird bird' -------------------------------------------------------------------------------- /06_decomposition_pattern/A_decomposing_problems_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Dividing and Conquering 2 | 3 | _Decomposition_ is a pattern that programmers use to take a complex problem and break it down into 4 | smaller sub-problems that can be managed better individually. 5 | 6 | Before you start this exercise, make sure you have watched the lecture first. 7 | 8 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 9 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 10 | 11 | This is might be my favorite topic. I think it's very high leverage because it is relatively easy to 12 | implement but greatly increases the elegance of your code. Prefer to solve complex problems with 13 | simple solutions. 14 | 15 | -AZ 16 | 17 | ## 0. Composition 18 | 19 | Create a new folder named `A_decomposing_problems_exercise`, this is where you'll save all of your 20 | files. Predict what would print when executing each of the snippets below. Test your prediction by 21 | typing the snippet into their own files and executing the program. Don't copy and paste. 22 | 23 | Write the below code into a file named `snippet-1.js`. Make a prediction for what it will print. Run 24 | it. 25 | 26 | ```js 27 | // snippet 1 28 | 29 | let flim = function () { 30 | console.log("leak"); 31 | flam(); 32 | console.log("geek"); 33 | }; 34 | 35 | let flam = function () { 36 | console.log("beak"); 37 | console.log("sneak"); 38 | }; 39 | 40 | flim(); 41 | ``` 42 | 43 | Write the below code into a file named `snippet-2.js`. Make a prediction for what it will print. Run 44 | it. 45 | 46 | ```js 47 | let alice = function (n) { 48 | for (let i = 0; i <= n; i++) { 49 | bob(i); 50 | } 51 | }; 52 | 53 | let bob = function (n) { 54 | if (n % 2 === 0) { 55 | console.log("good"); 56 | } else { 57 | console.log("bad"); 58 | } 59 | }; 60 | 61 | alice(4); 62 | ``` 63 | 64 | Write the below code into a file named `snippet-3.js`. Make a prediction for what it will print. Run 65 | it. 66 | 67 | ```js 68 | // snippet 3 69 | 70 | let foo = function () { 71 | for (let i = 0; i < 5; i++) { 72 | console.log("i = " + i); 73 | bar(); 74 | } 75 | }; 76 | 77 | let bar = function () { 78 | for (let j = 0; j < 5; j++) { 79 | console.log(" j = " + j); 80 | } 81 | }; 82 | 83 | foo(); 84 | ``` 85 | 86 | ## 1. Decomposition 87 | 88 | In this section of the exercise you'll be prompted to write a function to accomplish a specific 89 | task. Read the description and look at the example output to figure out what should be implemented 90 | within the function. Write each function to a separate file in the `A_decomposing_problems_exercise` 91 | folder and test your work by running the file. 92 | 93 | Some (but not all) of the problems below should be decomposed with a helper function. 94 | 95 | ### doubleVowel 96 | 97 | Create a `double-vowel.js` file and solve the function below: 98 | 99 | ```js 100 | // Write a function `doubleVowel` that accepts a string as an argument. The function should return 101 | // the string where every occurrence of a vowel in the original string is repeated twice consecutively. 102 | // Vowels are the letters a, e, i, o, u. 103 | 104 | console.log(doubleVowel("runner")); // 'ruunneer' 105 | console.log(doubleVowel("stoplight")); // 'stoopliight' 106 | console.log(doubleVowel("gardener")); // 'gaardeeneer' 107 | ``` 108 | 109 | ### funnyPhrase 110 | 111 | Create a `funny-phrase.js` file and solve the function below: 112 | 113 | ```js 114 | // Write a function `funnyPhrase` that accepts a sentence string as an argument. The function should 115 | // return the sentence where every other word has its vowels repeated twice consecutively. 116 | // Vowels are the letters a, e, i, o, u. 117 | 118 | console.log(funnyPhrase("she dreamed of being a runner")); // 'she dreeaameed of beeiing a ruunneer' 119 | console.log(funnyPhrase("park near the stoplight")); // 'park neeaar the stoopliight' 120 | console.log(funnyPhrase("we need many gardeners")); // 'we neeeed many gaardeeneers' 121 | ``` 122 | 123 | ### isPrime 124 | 125 | Create an `is-prime.js` file and solve the function below: 126 | 127 | ```js 128 | // Write a function `isPrime` that accepts a number as an argument. The function should return a 129 | // boolean indicating whether or not the given number is prime. A prime number is a number that is 130 | // only divisible by 1 and itself. The smallest prime number is 1. 131 | // 132 | // For example, 11 is a prime because it is only divisible by 1 and 11. 133 | // For example, 8 is not a prime because it is divisible by 1, 2, 4, and 8 134 | 135 | console.log(isPrime(11)); // true 136 | console.log(isPrime(8)); // false 137 | console.log(isPrime(7)); // true 138 | console.log(isPrime(21)); // false 139 | console.log(isPrime(2)); // true 140 | console.log(isPrime(15)); // false 141 | console.log(isPrime(1)); // false 142 | ``` 143 | 144 | ### pickPrimes 145 | 146 | Create a `pick-primes.js` file and solve the function below: 147 | 148 | ```js 149 | // Write a function `pickPrimes` that accepts an array of numbers as an argument. The function should 150 | // return an array containing only the prime numbers. A prime number is a number that is 151 | // only divisible by 1 and itself. The smallest prime number is 1. 152 | 153 | console.log(pickPrimes([12, 3, 7, 18, 11])); // [3, 7, 11] 154 | console.log(pickPrimes([17, 23, 9, 42])); // [17, 23] 155 | console.log(pickPrimes([4, 2048, 100, 55])); // [] 156 | ``` 157 | 158 | ## Walkthrough 159 | 160 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 161 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 162 | Watch it in 1.5x speed if you like. 163 | -------------------------------------------------------------------------------- /06_decomposition_pattern/B_decomposing_problems_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Predisposed to Decompose 2 | 3 | More practice on decomposing! 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | Write code. Run it. 9 | 10 | -AZ 11 | 12 | ## Instructions 13 | 14 | Create a new folder named `B_decomposing_problems_exercise`, this is where you'll save all of your 15 | files. Solve each of the prompts below. Create a new file for each prompt and execute your code to 16 | see if it matches the example output. 17 | 18 | Some (but not all) of the problems below should be decomposed with a helper function. 19 | 20 | ### removeLastVowel 21 | 22 | Create a `remove-last-vowel.js` file and solve the function below: 23 | 24 | ```js 25 | // Write a function `removeLastVowel` that accepts a string as argument. The function should return 26 | // the string with its last vowel removed. Vowels are the letters a, e, i, o, u 27 | 28 | console.log(removeLastVowel("speaker")); // 'speakr' 29 | console.log(removeLastVowel("trading")); // 'tradng' 30 | console.log(removeLastVowel("thunder")); // 'thundr' 31 | console.log(removeLastVowel("myth")); // 'myth' 32 | ``` 33 | 34 | ### pickPrefix 35 | 36 | Create a `pick-prefix.js` file and solve the function below: 37 | 38 | ```js 39 | // Write a function `pickPrefix(strings, prefix)` that accepts an array of strings and a prefix string 40 | // as an argument. The function should return the an array of words that begin with the prefix. 41 | 42 | console.log(pickPrefix(['connect', 'company', 'concert', 'cram'], 'con')); 43 | // ['connect', 'concert'] 44 | 45 | console.log(pickPrefix(['miner', 'mistake', 'misspeak', 'moose', 'mission'], 'mis')); 46 | // [''mistake', 'misspeak', 'mission'] 47 | ``` 48 | 49 | ### trendyText 50 | 51 | Create a `trendy-text.js` file and solve the function below: 52 | 53 | ```js 54 | // Write a function `trendyText` that accepts a sentence string as an argument. The function should 55 | // return the sentence where the last vowel of every word is removed. 56 | 57 | console.log(trendyText("the concert will be epic")); // 'th concrt wll be epc' 58 | console.log(trendyText("breakfast food is wonderful")); // 'breakfst fod s wonderfl' 59 | console.log(trendyText("the weather will improve hopefully")); // 'th weathr wll improv hopeflly' 60 | ``` 61 | 62 | ### mostLetterWord 63 | 64 | Create a `most-letter-word` file and solve the function below: 65 | 66 | ```js 67 | // Write a function `mostLetterWord` that accepts a sentence string and a character as arguments. 68 | // The function should return the word of the sentence that contains the character the most number 69 | // of times. You can assume that the sentence contains at least one word. If there is a tie, return 70 | // the word that appears earlier in the sentence. 71 | 72 | console.log(mostLetterWord('she received an award for excellence in science', 'e')); // 'excellence' 73 | console.log(mostLetterWord('she received an award for excellence in science', 'a')); // 'award' 74 | console.log(mostLetterWord('I hope sophomore year comes soon', 'o')); // 'sophomore' 75 | console.log(mostLetterWord('I hope sophomore year comes soon', 's')); // 'sophomore' 76 | ``` 77 | 78 | ## Walkthrough 79 | 80 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 81 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 82 | Watch it in 1.5x speed if you like. 83 | -------------------------------------------------------------------------------- /06_decomposition_pattern/C_decomposing_problems_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Building It Up By Breaking It Down 2 | 3 | Three reasons why you'll improve as a programmer. You won't believe number 3! 4 | 5 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 6 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 7 | 8 | I'm going to make you practice so much that you wish you had never subscribed. 9 | 10 | -AZ 11 | 12 | ## Instructions 13 | 14 | Create a new folder named `C_decomposing_problems_exercise`, this is where you'll save all of your 15 | files. Solve each of the prompts below. Create a new file for each prompt and execute your code to 16 | see if it matches the example output. 17 | 18 | ### lalaLanguage 19 | 20 | Create a `lala-language.js` file and solve the function below: 21 | 22 | ```js 23 | // Write a function `lalaLanguage` that accepts a sentence string as an argument. The function should 24 | // return a new sentence where words longer that 3 characters are modified. Modified words should have 25 | // each vowel followed by 'l' and the same vowel repeated again. See the examples. 26 | 27 | console.log(lalaLanguage('this is pretty strange')); // 'thilis is preletty stralangele' 28 | console.log(lalaLanguage('can you speak our language')); // 'can you spelealak our lalangulualagele' 29 | ``` 30 | 31 | ### pickPerfectSquares 32 | 33 | Create a `pick-perfect-squares.js` file and solve the function below: 34 | 35 | ```js 36 | // Write a function `pickPerfectSquares` that accepts an array of numbers as an argument. The function 37 | // should return an array containing only the elements that are perfect squares. A perfect square 38 | // is a number that can result from multiplying some number by itself. 39 | // 40 | // For example, 4 is a perfect square because 2 * 2 = 4 41 | // For example, 81 is a perfect square because 9 * 9 = 81 42 | 43 | console.log(pickPerfectSquares([6, 4, 81, 21, 36])); // [4, 81, 36] 44 | console.log(pickPerfectSquares([100, 24, 144])); // [100, 144] 45 | console.log(pickPerfectSquares([30, 25])); // [25] 46 | ``` 47 | 48 | ### censorSentence 49 | 50 | Create a `censor-sentence.js` file and solve the function below: 51 | 52 | ```js 53 | // Write a function `censorSentence(sentence, targetWords)` that accepts a sentence string and an array 54 | // of target words as arguments. The function should return a new sentence where any target words 55 | // have all of their characters replaced with '*'s. 56 | 57 | console.log(censorSentence('where the heck is my celery', ['heck', 'celery'])); 58 | // 'where the **** is my ******' 59 | 60 | console.log(censorSentence('why you little sweetheart', ['sweetheart', 'salad'])); 61 | // 'why you little **********' 62 | ``` 63 | 64 | ## Walkthrough 65 | 66 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 67 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 68 | Watch it in 1.5x speed if you like. 69 | -------------------------------------------------------------------------------- /07_objects/A_objects_exercise/README.md: -------------------------------------------------------------------------------- 1 | # An Objective Perspective 2 | 3 | _Objects_ are a construct programmers use to represent entities and the properties they possess. 4 | Whereas arrays use numbers to index their values, _objects_ use strings to index their values. These 5 | strings are referred to as _keys_. 6 | 7 | Before you start this exercise, make sure you have watched the lecture first. 8 | 9 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 10 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 11 | 12 | As you grow into a web developer, you'll be dealing with objects a **ton**. I also like teaching 13 | objects because I get to use obscure references in my examples. 14 | 15 | -AZ 16 | 17 | ## 0. Objects In Mirror Are Closer Then They Appear. 18 | 19 | Create a new folder named `A_objects_exercise`, this is where you'll save all of your files. Predict 20 | what would print when executing each of the snippets below. Test your prediction by typing the 21 | snippet into their own files and executing the program. Don't copy and paste. 22 | 23 | Write the below code into a file named `snippet-1.js`. Make a prediction for what it will print. Run 24 | it. 25 | 26 | ```js 27 | // snippet 1 28 | let movie = { 29 | title: "Fight Club", 30 | year: 1999, 31 | genre: ["drama", "thriller"], 32 | starring: ["Brad Pitt", "Edward Norton"], 33 | }; 34 | 35 | console.log(movie.year); 36 | console.log(movie["title"]); 37 | console.log(movie.genre); 38 | console.log(movie.genre[0]); 39 | console.log(movie.genre[1]); 40 | 41 | console.log(movie.duration); 42 | console.log(movie["starring"][1]); 43 | console.log(movie.starring.length); 44 | ``` 45 | 46 | Write the below code into a file named `snippet-2.js`. Make a prediction for what it will print. Run 47 | 48 | ```js 49 | // snippet 2 50 | let restaurant = { 51 | name: "Bob's Burgers", 52 | location: "123 Ocean Avenue", 53 | owners: ["Bob Belcher", "Linda Belcher"], 54 | established: 2011, 55 | menu: ["burgers", "fries", "shakes"], 56 | }; 57 | 58 | console.log("owners" in restaurant); 59 | console.log("employees" in restaurant); 60 | 61 | let someKey = "menu"; 62 | console.log(someKey in restaurant); 63 | 64 | console.log(restaurant.menu); 65 | console.log(restaurant["menu"]); 66 | console.log(restaurant[someKey]); 67 | console.log(restaurant.someKey); 68 | 69 | console.log(restaurant.menu.includes("fries")); 70 | ``` 71 | 72 | Write the below code into a file named `snippet-3.js`. Make a prediction for what it will print. Run 73 | it. 74 | 75 | ```js 76 | // snippet 3 77 | let dog = { 78 | name: "Manny", 79 | age: 5, 80 | breed: "pug", 81 | color: "fawn", 82 | favoriteFoods: ["bacon"], 83 | }; 84 | 85 | console.log(dog.age); 86 | console.log(dog.breed); 87 | console.log(dog.favoriteFoods); 88 | 89 | dog.age++; 90 | dog.breed = dog.breed.toUpperCase(); 91 | dog.favoriteFoods.push("sausage"); 92 | 93 | console.log(dog.age); 94 | console.log(dog.breed); 95 | console.log(dog.favoriteFoods); 96 | 97 | for (let property in dog) { 98 | console.log(property + " is " + dog[property]); 99 | } 100 | ``` 101 | 102 | Write the below code into a file named `snippet-4.js`. Make a prediction for what it will print. Run 103 | it. 104 | 105 | ```js 106 | // snippet 4 107 | let recipe = { 108 | name: "Old Fashioned Pancakes", 109 | difficulty: "easy", 110 | tasty: true, 111 | ingredients: ["eggs", "milk", "butter", "flour", "sugar"], 112 | }; 113 | 114 | console.log(recipe.name); 115 | console.log(recipe["name"]); 116 | console.log(recipe.ingredients.length); 117 | console.log(recipe.calories); 118 | 119 | let someVariable = "difficulty"; 120 | console.log(recipe[someVariable]); 121 | console.log(recipe.someVariable); 122 | 123 | for (let i = 0; i < recipe.ingredients.length; i++) { 124 | console.log(recipe.ingredients[i]); 125 | } 126 | ``` 127 | 128 | ## 1. I Object, Your Honor. 129 | 130 | In this section of the exercise you'll be prompted to write a function to accomplish a specific 131 | task. Read the description and look at the example output to figure out what should be implemented 132 | within the function. Write each function to a separate file in the `A_objects_exercise` folder and 133 | test your work by running the file. 134 | 135 | ### emailParse 136 | 137 | Create a `email-parse.js` file and solve the function below: 138 | 139 | ```js 140 | // Create a function `emailParse` that accepts an email address string as an argument. The function 141 | // should return an object containing `username` and `domain` as keys. See the examples. 142 | // You can assume that the argument will be a valid email address. 143 | 144 | console.log(emailParse("coolcoder42@goodmail.com")); 145 | // { username: 'coolcoder42', domain: 'goodmail.com' } 146 | 147 | console.log(emailParse("az@woohoomail.com")); 148 | // { username: 'az', domain: 'woohoomail.com' } 149 | 150 | console.log(emailParse("1337pr0graMmer@coldmail.edu")); 151 | // { username: '1337pr0graMmer', domain: 'coldmail.edu' } 152 | ``` 153 | 154 | ### keyPair 155 | 156 | Create a `key-pair.js` file and solve the function below: 157 | 158 | ```js 159 | // Write a function `keyPair(obj1, obj2, key)` that accepts two objects and a key string as arguments. 160 | // The function should return an array containing the values of the given key in `obj1` and `obj2`. 161 | // See the examples. 162 | 163 | let cat1 = { name: "jinkee", breed: "calico" }; 164 | let cat2 = { name: "garfield", breed: "red tabby" }; 165 | console.log(keyPair(cat1, cat2, "breed")); // [ 'calico', 'red tabby' ] 166 | console.log(keyPair(cat1, cat2, "name")); // [ 'jinkee', 'garfield ] 167 | 168 | let sport1 = { name: "volleyball", team: true }; 169 | let sport2 = { name: "golf", team: false }; 170 | console.log(keyPair(sport1, sport2, "team")); // [ true, false ] 171 | ``` 172 | 173 | ### elementQuantities 174 | 175 | Create a `element-quantities.js` file and solve the function below: 176 | 177 | ```js 178 | // Create a function `elementQuantities` that accepts an object as an argument. The object contains 179 | // elements as keys and quantities as values. The function should return an array containing the elements 180 | // appearing with the correct quantities. See the examples. 181 | 182 | let quantities1 = { cat: 3, bird: 1, dog: 2 }; 183 | console.log(elementQuantities(quantities1)); // ['cat', 'cat', 'cat', 'bird, 'dog', 'dog'] 184 | 185 | let quantities2 = { blue: 3, brown: 1 }; 186 | console.log(elementQuantities(quantities2)); // ['blue', 'blue', 'blue', 'brown'] 187 | ``` 188 | 189 | ### maxObjectValue 190 | 191 | Create a `max-object-value.js` file and solve the function below: 192 | 193 | ```js 194 | // Write a function `maxObjectValue` that accepts an object as an argument. Write a function that 195 | // returns an array containing the key, value pair for the largest value within the object. You can 196 | // assume that all values are numbers and there are no ties. 197 | 198 | console.log(maxObjectValue({ a: 5, b: 2, c: 6, d: 7, e: 4 })); // ['d', 7] 199 | console.log(maxObjectValue({ lychee: 11, rambutan: 13, papaya: 9 })); // ['rambutan', 13] 200 | ``` 201 | 202 | ## Walkthrough 203 | 204 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 205 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 206 | Watch it in 1.5x speed if you like. 207 | -------------------------------------------------------------------------------- /07_objects/B_objects_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Object Obsession 2 | 3 | More practice on JavaScript objects! 4 | 5 | Before you start this exercise, make sure you have watched the lecture first. 6 | 7 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 8 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 9 | 10 | Keep practicing. Hopefully you are practicing everyday. Consistency beats intensity given a long 11 | enough timeline. If want to become proficient quickly, then practice intensely and consistently. 12 | 13 | -AZ 14 | 15 | ## Instructions 16 | 17 | Create a new folder named `B_objects_exercise`, this is where you'll save all of your files. Solve 18 | each of the prompts below. Create a new file for each prompt and execute your code to see if it 19 | matches the example output. 20 | 21 | ### characterCount 22 | 23 | Create a `character-count.js` file and solve the function below: 24 | 25 | ```js 26 | // Write a function `characterCount` that accepts a string as an argument. The function should return 27 | // an object containing the count of characters in the string. 28 | 29 | console.log(characterCount("evening")); // { e: 2, v: 1, n: 2, i: 1 , g: 1 } 30 | console.log(characterCount("mississippi")); // { m: 1, i: 4, s: 4, p: 2 } 31 | console.log(characterCount("chili")); // { c: 1, h: 1, i: 2, l: 1 } 32 | ``` 33 | 34 | ### letterMap 35 | 36 | Create a `letter-map.js` file and solve the function below: 37 | 38 | ```js 39 | // Write a function `letterMap` that accepts a string and an object as arguments. The function should 40 | // return a version of the string where characters that appear as keys in object are replaced with 41 | // their corresponding values. 42 | 43 | console.log(letterMap("symbolic", { y: "i", o: "a", c: "k" })); // 'simbalik' 44 | console.log(letterMap("colossal", { o: "x", s: "p" })); // 'cxlxppal' 45 | console.log(letterMap("miniscule", { u: "t", i: "f", e: "q" })); // 'mfnfsctlq' 46 | ``` 47 | 48 | ### mostCommonLetter 49 | 50 | Create a `most-common-letter.js` file and solve the function below: 51 | 52 | ```js 53 | // Write a function `mostCommonLetter` that accepts a string as an argument. The function should return 54 | // the character that appears most frequently in the string. You can assume that there are no ties. 55 | 56 | console.log(mostCommonLetter("building")); // 'i' 57 | console.log(mostCommonLetter("shoestring")); // 's' 58 | console.log(mostCommonLetter("preparedness")); // 'e' 59 | ``` 60 | 61 | ### wordReplace 62 | 63 | Create a `word-replace.js` file and solve the function below: 64 | 65 | ```js 66 | // Write a function `wordReplace` that accepts a sentence string and an object as arguments. The 67 | // function should return a new sentence where words that are keys of the object are replaced with 68 | // their corresponding values. 69 | 70 | console.log(wordReplace("I never take naps during the day", { never: "always", day: "weekend" })); 71 | // 'I always take naps during the weekend' 72 | 73 | console.log(wordReplace("the park is closed", { closed: "open", the: "a" })); 74 | // 'a park is open' 75 | 76 | console.log(wordReplace("I do what I want", { I: "we", cat: "dog" })); 77 | // 'we do what we want' 78 | ``` 79 | 80 | ### getAverageAge 81 | 82 | Create a `get-average-age.js` file and solve the function below: 83 | 84 | ```js 85 | // Write a function `getAverageAge` function that accepts an array of objects representing people. 86 | // The function should return the average age of the people. 87 | 88 | let peeps = [ 89 | { name: "Lovelace", age: 36, born: "London, UK" }, 90 | { name: "Kleene", age: 85, born: "Connecticut, US" }, 91 | { name: "Turing", age: 41, born: "London, UK" }, 92 | { name: "Hopper", age: 85, born: "New York, US" }, 93 | ]; 94 | console.log(getAverageAge(peeps)); // 61.75 95 | 96 | let people = [ 97 | { name: "Orwell", age: 46, born: "Bihar, India" }, 98 | { name: "Bradbury", age: 91, born: "California, US" }, 99 | { name: "Huxley", age: 69, born: "California, US" }, 100 | ]; 101 | console.log(getAverageAge(people)); // 86.66 102 | ``` 103 | 104 | ## Walkthrough 105 | 106 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 107 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 108 | Watch it in 1.5x speed if you like. 109 | -------------------------------------------------------------------------------- /07_objects/C_objects_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Obnoxious Objects 2 | 3 | Still more exercises on JavaScript objects! 4 | 5 | Before you start this exercise, make sure you have watched the lecture first. 6 | 7 | If you get stuck during this exercise, reference this walkthrough. Only watch enough of the 8 | walkthrough to get yourself unstuck, then pause the video and pick up the exercise on your own. 9 | 10 | Practice, practice, practice, practice. 11 | 12 | -AZ 13 | 14 | ## Instructions 15 | 16 | Create a new folder named `C_objects_exercise`, this is where you'll save all of your files. Solve 17 | each of the prompts below. Create a new file for each prompt and execute your code to see if it 18 | matches the example output. 19 | 20 | ### greatestPopulation 21 | 22 | Create a `greatest-population.js` file and solve the function below: 23 | 24 | ```js 25 | // Write a function `greatestPopulation` that accepts an array of country objects as an argument. 26 | // The function should return the name of the country that has the largest population. 27 | // You can assume that the array contains at least one country. 28 | 29 | let countries1 = [ 30 | { name: "Cameroon", population: 27744989, gdp: 38.68 }, 31 | { name: "Belarus", population: 9477918, gdp: 59.66 }, 32 | { name: "Indonesia", population: 267026366, gdp: 1042 }, 33 | { name: "Guyana", population: 750204, gdp: 3.88 }, 34 | ]; 35 | 36 | console.log(greatestPopulation(countries1)); // 'Indonesia' 37 | 38 | let countries1 = [ 39 | { name: "New Zealand", population: 4925477, gdp: 204.9 }, 40 | { name: "Mozambique", population: 30098197, gdp: 14.72 }, 41 | { name: "Greenland", population: 57616, gdp: 2.71 }, 42 | { name: "Kazakhstan", population: 19091949, gdp: 179.3 }, 43 | { name: "Burma", population: 56590071, gdp: 71.21 }, 44 | ]; 45 | 46 | console.log(greatestPopulation(countries2)); // 'Burma' 47 | ``` 48 | 49 | ### pluck 50 | 51 | Create a `pluck.js` file and solve the function below: 52 | 53 | ```js 54 | // Write a function `pluck` that accepts an object and an array of strings as an argument. The function 55 | // should return a new object containing the keys of the original object that are found in the array. 56 | 57 | console.log(pluck({name: 'Fido', color: 'Brown', breed: 'German Shepherd'}, ['name', 'breed'])); 58 | // { name: 'Fido', breed: 'German Shepherd' } 59 | 60 | console.log(pluck({ make: 'Tesla', mpg: 93 , model: 'Model X', color: 'white' }, ['make', 'model'])); 61 | // { make: 'Tesla', model: 'Model X' } 62 | ``` 63 | 64 | ### objectAdd 65 | 66 | Create a `object-add.js` file and solve the function below: 67 | 68 | ```js 69 | // Write a function `objectAdd` that accepts two objects as arguments. The function should return 70 | // a new object containing the same keys as the object arguments. If a key is found in both 71 | // objects, its corresponding value should be the sum of the values in the object arguments. If a key 72 | // is only found in one of the objects, then it's corresponding value should be the same as that one 73 | // object. 74 | 75 | let obj1 = { x: 3, y: 10}; 76 | let obj2 = { y: 2, x: 1}; 77 | console.log(objectAdd(obj1, obj2)); // { x: 4, y: 12 } 78 | 79 | let obj3 = { a: 3, b: 2, c: -1}; 80 | let obj4 = { b: 5, c: 1, e: 4}; 81 | console.log(objectAdd(obj3, obj4)); // { a: 3, b: 7, c: 0, e: 4 } 82 | ``` 83 | 84 | ### secretCipher 85 | 86 | Create a `secret-cipher.js` file and solve the function below: 87 | 88 | ```js 89 | // Write a function `secretCipher` that accepts a string and an object as arguments. The function 90 | // should return a new string where characters of the original string are replaced with their 91 | // corresponding values in the object. If a character of the string does not exist as a key of the 92 | // object, then it should be replaced with a question mark (?). 93 | 94 | console.log(secretCipher("jello", { j: "r", l: "s", e: "i" })); // 'riss?' 95 | console.log(secretCipher("lantern", { e: "o", l: "p", n: "m", r: "j" })); // 'p?m?ojm' 96 | ``` 97 | 98 | ## Walkthrough 99 | 100 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 101 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 102 | Watch it in 1.5x speed if you like. 103 | -------------------------------------------------------------------------------- /08_higher_order_functions/A_native_callback_methods_exercise/README.md: -------------------------------------------------------------------------------- 1 | # Native Callback Methods Exercise 2 | 3 | There are a handful of useful array methods utilizing callbacks that are built into JavaScript. The 4 | `forEach`, `map`, `filter`, `every`, and `some` methods can implement common patterns 5 | quickly. These methods can greatly clean up our code if we master their usage. 6 | 7 | In my experience teaching, students are somewhat resistant to learning this new topic because they 8 | can "solve" these same problems utilizing just the basics that they are already comfortable with. 9 | You _could_ forgo these methods in favor of manually implementing the target behavior with a 10 | nothing-fancy loop, however these methods will become very useful if you have the patience to learn 11 | them. They are also a fairly common topic in software engineering job interviews. There is much 12 | discomfort that comes with learning something new. Practice until it's comfortable. 13 | 14 | -AZ 15 | 16 | ### 0. Interpret. 17 | 18 | Create a new folder named `A_native_callback_methods_exercise`, this is where you'll save all of 19 | your files. Predict what would print when executing each of the snippets below. Test your prediction 20 | by typing the snippet into their own files and executing the program. Don't copy and paste. 21 | 22 | Write the below code into a file named `snippet-1.js`. Make a prediction for what it will print. Run 23 | it. 24 | 25 | ```js 26 | // snippet-1 27 | let foo = function (s) { 28 | return s.slice(1).toUpperCase(); 29 | }; 30 | 31 | let arr = ['mashy', 'brando', 'thanasi']; 32 | let res = arr.map(foo); 33 | console.log(res); 34 | ``` 35 | 36 | ```js 37 | // snippet-2 38 | let friends = ['nader', 'matty', 'jennifer', 'lora']; 39 | 40 | let chosen = friends[0]; 41 | 42 | friends.forEach(function (friend) { 43 | if (friend.length > chosen.length) { 44 | chosen = friend; 45 | } 46 | }); 47 | 48 | console.log(chosen); 49 | ``` 50 | 51 | ```js 52 | // snippet-3 53 | let isEven = function (n) { 54 | return n % 2 === 0; 55 | }; 56 | 57 | let isOdd = function (n) { 58 | return !isEven(n); 59 | }; 60 | 61 | let nums1 = [42, 3, 8, 9]; 62 | console.log(nums1.filter(isEven)); 63 | console.log(nums1.map(isOdd)); 64 | ``` 65 | 66 | ```js 67 | // snippet-4 68 | let doggos = [ 69 | { name: 'bella', type: 'canine', age: 4, breed: 'yorkshire terrier' }, 70 | { name: 'manny', type: 'canine', age: 2, breed: 'pug' }, 71 | { name: 'yoshi', type: 'canine', age: 3, breed: 'german shepherd' }, 72 | { name: 'blossom', type: 'canine', age: 3, breed: 'german shepherd' } 73 | ]; 74 | 75 | let result1 = doggos.every(function (ele) { 76 | return ele.type === 'canine'; 77 | }); 78 | 79 | console.log(result1); 80 | 81 | let result2 = doggos.every(function (ele) { 82 | return ele.breed === 'german shepherd'; 83 | }); 84 | 85 | console.log(result2); 86 | 87 | let result3 = doggos.some(function (ele) { 88 | return ele.breed === 'german shepherd'; 89 | }); 90 | 91 | console.log(result3); 92 | 93 | let isOld = function (person) { 94 | return person.age > 5; 95 | }; 96 | 97 | let result4 = doggos.some(isOld); 98 | 99 | console.log(result4); 100 | ``` 101 | 102 | ### 1. Solve. 103 | 104 | Solve each of the prompts below. Create a new file for each prompt and execute your code to see if it 105 | matches the example output. 106 | 107 | ### forEachSum 108 | 109 | Create a `for-each-sum.js` file and solve the function below: 110 | 111 | ```js 112 | // Write a function `forEachSum` that accepts an array of numbers as an argument and returns the total 113 | // sum of all numbers. 114 | // Your function must utilize the native Array#forEach method. 115 | 116 | console.log(forEachSum([10, 3, 5])); // 18 117 | console.log(forEachSum([-6, 7, 9, 4])); // 14 118 | console.log(forEachSum([])); // 0 119 | ``` 120 | 121 | ### pickAdults 122 | 123 | Create a `pick-adults.js` file and solve the function below: 124 | 125 | ```js 126 | // Write a function `pickAdults` that accepts an array of person objects and returns the objects that 127 | // have an age of at least 18. 128 | // Your function must utilize the native Array#filter method. 129 | 130 | let people = [ 131 | { name: 'alice', age: 22 }, 132 | { name: 'bob', age: 37 }, 133 | { name: 'pam', age: 16 }, 134 | { name: 'jim', age: 18 } 135 | ]; 136 | console.log(pickAdults(people)); 137 | // [ { name: 'alice', age: 22 }, { name: 'bob', age: 37 }, { name: 'jim', age: 18 } ] 138 | 139 | let people2 = [ 140 | { name: 'betty', age: 23 }, 141 | { name: 'ben', age: 15 }, 142 | { name: 'yansi', age: 21 } 143 | ]; 144 | console.log(pickAdults(people2)); 145 | // [ { name: 'betty', age: 23 }, { name: 'yansi', age: 21 } ] 146 | ``` 147 | 148 | ### wordMap 149 | 150 | Create a `word-map.js` file and solve the function below: 151 | 152 | ```js 153 | // Write a function `wordMap` that accepts a sentence string and an object as arguments. The function 154 | // should return a new sentence where words that exist as keys in the object are replaced with their 155 | // corresponding values. 156 | // Your function must utilize the native Array#map method. 157 | 158 | console.log(wordMap('open the pod bay doors', {pod: 'ship', open: 'close'})); 159 | // 'close the ship bay doors' 160 | 161 | console.log(wordMap('breakfast food is good', {breakfast: 'brunch', good: 'great'})); 162 | // 'brunch food is great' 163 | ``` 164 | 165 | ### commonElements 166 | 167 | Create a `common-elements.js` file and solve the function below: 168 | 169 | ```js 170 | // Write a function `commonElements` that accepts two arrays as arguments. The function should 171 | // return a new array containing the elements that are found in both input arrays. 172 | // Your function must utilize the native Array#filter method. 173 | 174 | console.log(commonElements(['a', 'b', 'c'], ['c', 'a'])); // ['a', 'c'] 175 | console.log(commonElements(['cat', 'dog', 'mouse', 'fish'], ['dog', 'rat'])); // ['dog'] 176 | console.log(commonElements(['skip', 'jump'], ['swim', 'hop'])); // [] 177 | ``` 178 | 179 | 180 | ### getInitials 181 | 182 | Create a `get-initials.js` file and solve the function below: 183 | 184 | ```js 185 | // Write a function `getInitials` that accepts an array of students as an argument. The function should 186 | // return an array containing the initials of each student. 187 | // Your function must utilize the native Array#map method. 188 | 189 | let students1 = [ 190 | { first: 'ada', last: 'yonath', subject: 'chemistry' }, 191 | { first: 'nelly', last: 'sachs', subject: 'literature' }, 192 | { first: 'rosalyn', last: 'yallow', subject: 'medicine' } 193 | ]; 194 | console.log(getInitials(students1)); // ['AY', 'NS', 'RY'] 195 | 196 | 197 | let students2 = [ 198 | { first: 'margaret', last: 'knight' }, 199 | { first: 'ellen', last: 'ochoa' }, 200 | ]; 201 | console.log(getInitials(students2)); // ['MK', 'EO'] 202 | ``` 203 | 204 | ## Walkthrough 205 | 206 | Watch the walkthrough in it's entirety so you don't miss out on any knowledge. Still watch the full 207 | walkthrough even if you were able to complete this exercise easily, so you don't miss any tips. 208 | Watch it in 1.5x speed if you like. 209 | -------------------------------------------------------------------------------- /08_higher_order_functions/B_native_callback_methods_exercise/README.md: -------------------------------------------------------------------------------- 1 | ```js 2 | // snippet-5 3 | 4 | let nums = [3, 7, 2, 9]; 5 | 6 | let res1 = nums.reduce(function (a, b) { 7 | return a + b; 8 | }); 9 | 10 | console.log(res1); 11 | 12 | 13 | let res2 = nums.reduce(function (a, b) { 14 | return a + b; 15 | }, -10); 16 | 17 | console.log(res2); 18 | 19 | let res3 = nums.reduce(function (a, b) { 20 | if (b < a) { 21 | return b; 22 | } else { 23 | return a; 24 | } 25 | }); 26 | 27 | console.log(res3); 28 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # learn-javascript --------------------------------------------------------------------------------