├── README.md ├── module_02.js └── module_01.js /README.md: -------------------------------------------------------------------------------- 1 | # Meta-Programming-With-JavaScript 2 | This repo contains the track record for my practice of JavaScript following the Meta: Programming With JavaScript course from Coursera. 3 | -------------------------------------------------------------------------------- /module_02.js: -------------------------------------------------------------------------------- 1 | /* ------------------------- Functions ------------------------- */ 2 | 3 | /* ------------------- Letter Finder Function ------------------- */ 4 | 5 | function letterFinder(word, match) { 6 | for (var i = 0; i < word.length; i++){ 7 | if(word[i] == match){ 8 | console.log('Found the', match, 'at', i) 9 | } 10 | else{ 11 | console.log('---No match found at', i) 12 | } 13 | } 14 | } 15 | letterFinder("test", "t"); 16 | 17 | -------------------------------------------------------------------------------- /module_01.js: -------------------------------------------------------------------------------- 1 | /* ------------------------- Variables ------------------------- */ 2 | 3 | var petDog = 'Rex'; 4 | var petCat = 'Pepper'; 5 | console.log(petDog); 6 | console.log(petCat); 7 | console.log('My pet dog name is', petDog); 8 | console.log('My pet cat name is', petCat); 9 | var dogSound = 'woof'; 10 | var catSound = 'purr'; 11 | console.log(petDog, 'says', dogSound); 12 | console.log(petCat, 'says', catSound); 13 | catSound = 'meow'; 14 | console.log(petCat, 'now says', catSound); 15 | 16 | /* ------------------------- Task 1: Using the logical && operator ------------------------- */ 17 | 18 | var score = 8; 19 | console.log('Mid-level skills:', score > 0 && score < 10); 20 | 21 | /* ------------------------- Task 2: Using the logical || operator ------------------------- */ 22 | 23 | var timeRemaining = 0; 24 | var energy = 10; 25 | console.log('Game over:', timeRemaining == 0 || energy == 0); 26 | 27 | /* ------------------------- Task 3: Using the modulus operator, %, to test if a given number is odd ------------------------- */ 28 | 29 | var num1 = 2; 30 | var num2 = 5; 31 | var test1 = num1 % 2; 32 | var test2 = num2 % 2; 33 | var result1 = test1 == 0; 34 | var result2 = test2 == 0; 35 | console.log("Is", num1, "an even number?", result1); 36 | console.log("Is", num2, "an even number?", result2); 37 | 38 | 39 | /* ------------------------- Task 4: Add numbers using the + operator ------------------------- */ 40 | 41 | console.log(5+10); 42 | 43 | /* ------------------------- Task 5: Concatenate numbers and strings using the + operator ------------------------- */ 44 | 45 | var now = "Now in "; 46 | var three = 3; 47 | var d = "D!"; 48 | console.log(now + three + d); 49 | 50 | 51 | /* ------------------------- Task 6: Use the += operator to accumulate values in a variable ------------------------- */ 52 | 53 | var counter = 0; 54 | counter += 5; 55 | counter += 3; 56 | console.log(counter); 57 | 58 | 59 | /* ------------------------- Conditional Statements ------------------------- */ 60 | 61 | var age = 10; 62 | if (age >= 65) 63 | { 64 | console.log("You get your income from your pension"); 65 | } 66 | else if (age < 65 && age >= 18) 67 | { 68 | console.log("Each month you get a salary"); 69 | } 70 | else if (age < 18) 71 | { 72 | console.log("You get an allowance"); 73 | } 74 | else 75 | { 76 | console.log("The value of the age variable is not numerical"); 77 | } 78 | 79 | /* --------------------------------------------------------------------------- */ 80 | 81 | var day = "Thursday"; 82 | switch (day) 83 | { 84 | case 'Monday': 85 | { 86 | console.log("It's Monday!"); 87 | break; 88 | } 89 | case 'Tuesday': 90 | { 91 | console.log("It's Tuesday!"); 92 | break; 93 | } 94 | case 'Wednesday': 95 | { 96 | console.log("It's Wednesday!"); 97 | break; 98 | } 99 | case 'Thursday': 100 | { 101 | console.log("It's Thursday!"); 102 | break; 103 | } 104 | case 'Friday': 105 | { 106 | console.log("It's Friday!"); 107 | break; 108 | } 109 | case 'Saturday': 110 | { 111 | console.log("It's Saturday!"); 112 | break; 113 | } 114 | case 'Sunday': 115 | { 116 | console.log("It's Sunday!"); 117 | break; 118 | } 119 | default: 120 | console.log('There is no such day'); 121 | } 122 | 123 | /* ------------------------- For Loop ------------------------- */ 124 | 125 | for (var i = 0; i <= 4; i++) 126 | { 127 | console.log(i+1); 128 | } 129 | console.log("Counting completed!"); 130 | 131 | /* --------------------------------------------------------------------------- */ 132 | 133 | for (var i = 5; i >= 1; i--) 134 | { 135 | console.log(i); 136 | } 137 | console.log("Countdown finished!"); 138 | 139 | /* ------------------------- While Loop ------------------------- */ 140 | 141 | var i = 1; 142 | while (i <= 5) 143 | { 144 | console.log(i); 145 | i++; 146 | } 147 | console.log("Counting completed!"); 148 | 149 | /* --------------------------------------------------------------------------- */ 150 | 151 | var i = 5; 152 | while(i >= 1) 153 | { 154 | console.log(i); 155 | i--; 156 | } 157 | console.log("Countdown finished!"); 158 | 159 | /* --------------------------------------------------------------------------- */ 160 | 161 | var year = 2018; 162 | while(year <= 2022) 163 | { 164 | console.log(year); 165 | year++; 166 | } 167 | 168 | /* ------------------------- Nested Loop ------------------------- */ 169 | 170 | var cubes = 'ABCDEFG'; 171 | //styling console output using CSS with a %c format specifier 172 | for (var i = 0; i < cubes.length; i++) { 173 | var styles = "font-size: 40px; border-radius: 10px; border: 1px solid blue; background: pink; color: purple"; 174 | console.log("%c" + cubes[i], styles) 175 | } 176 | // => run this code in the console of browser to run properly 177 | 178 | /* --------------------------------------------------------------------------- */ 179 | 180 | /* Exercise 1 181 | 182 | In this exercise, you will create the code for a for loop, using the counter variable named i starting from 1. 183 | 184 | To make the counter increment by 1 on each loop, you will use i++. 185 | 186 | The exit condition for the for loop should match the output given below. 187 | 188 | Inside the loop, write an if-else statement, which will check the following conditions: 189 | 190 | First, it will check if the value of i is 1. If it is, your code will console log the string "Gold medal". 191 | 192 | Next, I will check if the value of i is 2. If it is, your code will console log the string "Silver medal". 193 | 194 | Then, your code will check if the value of i is 3. If it is, it will console log the string "Bronze medal". 195 | 196 | For all the remaining values of i, your code will console log just the value of i. 197 | 198 | Note: The expected console log of the entire code should be as follows. 199 | Gold medal 200 | Silver medal 201 | Bronze medal 202 | 4 203 | 5 204 | 6 205 | 7 206 | 8 207 | 9 208 | 10 */ 209 | 210 | for (var i = 1; i <= 10; i++) 211 | { 212 | if(i == 1) 213 | { 214 | console.log("Gold Medal"); 215 | } 216 | else if(i == 2) 217 | { 218 | console.log("Silver Medal"); 219 | } 220 | else if(i == 3) 221 | { 222 | console.log("Bronze Medal"); 223 | } 224 | else 225 | { 226 | console.log(i) 227 | } 228 | } 229 | 230 | /* --------------------------------------------------------------------------- */ 231 | 232 | /* Exercise 2. Use the completed code from the previous task, but convert the conditionals to a switch statement. 233 | When you code the solution, the output in the console should remain exactly the same as in the previous question. 234 | 235 | Note: You'll need three separate cases for the three medals, and a default case for all other values of the i variable. */ 236 | 237 | for (var i = 1; i <= 10; i++) 238 | { 239 | switch(i) 240 | { 241 | case 1: 242 | console.log("Gold Medal"); 243 | break; 244 | case 2: 245 | console.log("Silver Medal"); 246 | break; 247 | case 3: 248 | console.log("Bronze Medal"); 249 | break; 250 | default: 251 | console.log(i); 252 | } 253 | } --------------------------------------------------------------------------------