├── .DS_Store ├── README.md ├── session1 ├── index_extra.js ├── index_finished.js └── index_start.js ├── session2 ├── index_extra.js ├── index_finished.js └── index_start.js └── session3 ├── index_extra.js ├── index_finished.js └── index_start.js /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bradtraversy/javascript_cardio/54e3644d578d004eb121a67af7bc82ba5f25c4b9/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript Cardio 2 | 3 | > Intermediate JavaScript challenges. 4 | 5 | Feel free to make a pull request, but please use the `index_extra.js` file. Comment out your solution and leave "Solution By [YOUR NAME]". Please keep the file neat and do NOT change anyone else's solutions. 6 | -------------------------------------------------------------------------------- /session1/index_extra.js: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | // CHALLENGE 1: REVERSE A STRING 3 | // Return a string in reverse 4 | // ex. reverseString('hello') === 'olleh' 5 | 6 | function reverseString(str) { 7 | // CONTRIBUTED SOLUTION 8 | // let arr = [...str]; 9 | // let newArr = arr.map((_, i, arr) => { 10 | // return arr[arr.length - 1 - i]; 11 | // }); 12 | // return newArr.join(""); 13 | // CONTRIBUTED SOLUTION 14 | // BY Ankita Patil 15 | // return str.split("").reduce((reversedString, character) => character + reversedString, ""); 16 | 17 | // ================= 18 | // CONTRIBUTED SOLUTION 19 | // if(str === ""){ 20 | // return str 21 | // }else{ 22 | // return reverseString(str.substr(1)) + str[0] 23 | // } 24 | // .... BY Yusuf Delvi 25 | // https://github.com/yusuf-delvi 26 | // ================== 27 | 28 | // ================= 29 | // CONTRIBUTED SOLUTION 30 | // let arr = [...str]; 31 | // 32 | // return arr.reduce((accumulator, currentValue, currentIndex, array) => { 33 | // return accumulator + array[array.length - currentIndex - 1]; 34 | // }, ""); 35 | // .... BY Jawad Mohammad 36 | // https://github.com/jawadiut 37 | // ================== 38 | 39 | // ================= 40 | // CONTRIBUTED SOLUTION 41 | // by Gamze Erol 42 | // return [...str].reverse().join(''); 43 | // ================= 44 | 45 | // ================= 46 | // CONTRIBUTED SOLUTION 47 | // by Lalit Kumar 48 | // if (str === '') { 49 | // return ''; 50 | // } else { 51 | // return reverseString(str.substr(1)) + str.charAt(0); 52 | // } 53 | // ================== 54 | } 55 | 56 | // CHALLENGE 2: VALIDATE A PALINDROME 57 | // Return true if palindrome and false if not 58 | // ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false 59 | 60 | function isPalindrome(str) { 61 | // CONTRIBUTED SOLUTION 62 | // let isPali = true; 63 | // let caseStr = str.toLowerCase() 64 | // let arr = caseStr.split('') 65 | // for(let i = 0; i < arr.length; i++){ 66 | // if(arr[i] !== arr[arr.length -1 -i]){ 67 | // isPali = false; 68 | // break; 69 | // } 70 | // } 71 | // return isPali 72 | 73 | // CONTRIBUTED SOLUTION 74 | // for (i=0; i char === newStr[newStr.length - 1 - index]); 95 | // ================== 96 | 97 | } 98 | 99 | // CHALLENGE 3: REVERSE AN INTEGER 100 | // Return an integer in reverse 101 | // ex. reverseInt(521) === 125 102 | 103 | function reverseInt(int) { 104 | // CONTRIBUTED SOLUTION 105 | // BY Abdulahi Roble 106 | // Github: abdu020 107 | // function reverseInt(int) { 108 | // const revNumber = int 109 | // .split("") 110 | // .reverse() 111 | // .map(function(t) { 112 | // return parseInt(t); 113 | // }); 114 | // return revNumber; 115 | // } 116 | 117 | // ===================== 118 | // CONTRIBUTED SOLUTION 119 | // by Gamze Erol 120 | //return parseInt([...int.toString()].reverse().join('')); 121 | // ===================== 122 | 123 | } 124 | 125 | // CHALLENGE 4: CAPITALIZE LETTERS 126 | // Return a string with the first letter of every word capitalized 127 | // ex. capitalizeLetters('i love javascript') === 'I Love Javascript' 128 | function capitalizeLetters(str) { 129 | // CONTRIBUTED SOLUTION 130 | // By Flinchy 131 | 132 | // let splitStr = str.split(' '); 133 | // let newStr = []; 134 | // let firstLetter, capitalizeFirstLetter, word, capitalizeWord; 135 | 136 | // for(let i = 0; i < splitStr.length; i++) { 137 | // word = splitStr[i]; 138 | // firstLetter = word[0]; 139 | // capitalizeFirstLetter = firstLetter.toUpperCase(); 140 | // capitalizeWord = word.replace(firstLetter, capitalizeFirstLetter); 141 | // newStr.push(capitalizeWord); 142 | // } 143 | // return newStr.join(' '); 144 | // ========================== 145 | // CONTRIBUTED SOLUTION 146 | // by Gamze Erol 147 | //return str.split(' ').map(s => 148 | // s[0].toUpperCase() + s.substring(1)).join(' '); 149 | //=============================== 150 | 151 | // CONTRIBUTED SOLUTION 152 | // by Lalit Kumar 153 | // return str 154 | // .split(" ") 155 | // .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) 156 | // .join(" "); 157 | 158 | //===============================// 159 | } 160 | 161 | // CHALLENGE 5: MAX CHARACTER 162 | // Return the character that is most common in a string 163 | // ex. maxCharacter('javascript') == 'a' 164 | function maxCharacter(str) { 165 | // CONTRIBUTED SOLUTION 166 | // Here is my version of the algorithm. 167 | // I put the string into an array and I sort it. 168 | // Then I loop through the array keeping track of the count for each character. 169 | // I keep track of the "current character" and the "max character" with objects. 170 | // const sortedStr = str.split('').sort(); 171 | // const maxChar = {char: '', count: 0}; 172 | // const currentChar = {char: '', count: 0}; 173 | // sortedStr.forEach(char => { 174 | // if(currentChar.char === char){ 175 | // currentChar.count++; 176 | // } else { 177 | // if(maxChar.count < currentChar.count){ 178 | // maxChar.char = currentChar.char; 179 | // maxChar.count = currentChar.count; 180 | // } 181 | // currentChar.char = char; 182 | // currentChar.count = 0; 183 | // } 184 | // }); 185 | // return maxChar.char; 186 | } 187 | 188 | //Solution by Naomi Sharp 189 | //Fixes bug in above solution for which the string "socks" would not work. This is because 190 | //the char "s" is the last one in the array sortedStr so the code to store maxChar does not run. 191 | 192 | function maxCharacter(str) { 193 | // const sortedStr = str.split('').sort(); 194 | // const maxChar = {char: '', count: 0}; 195 | // const currentChar = {char: '', count: 0}; 196 | 197 | // sortedStr.forEach(char => { 198 | // if(currentChar.char === char){ 199 | // currentChar.count++; 200 | // if(maxChar.count < currentChar.count){ 201 | // maxChar.char = currentChar.char; 202 | // maxChar.count = currentChar.count; 203 | // } 204 | // currentChar.char = char; 205 | // currentChar.count = 0; 206 | // } else { 207 | // currentChar.char = char; 208 | // currentChar.count = 1; 209 | // } 210 | // }); 211 | 212 | // return maxChar.char; 213 | } 214 | 215 | // CHALLENGE 6: FIZZBUZZ 216 | // Write a program that prints all the numbers from 1 to 100. For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". 217 | function fizzBuzz() { 218 | //CONTRIBUTED SOLUTION 219 | // By Devesh 220 | // let c3 = 0; 221 | // let c5 = 0; 222 | // for(let i = 1; i <= 100 ; i++){ 223 | // c3++; 224 | // c5++; 225 | // let d = ''; 226 | // if(c3 == 3){ 227 | // d += 'Fizz'; 228 | // c3 = 0; 229 | // } 230 | // if(c5 == 5){ 231 | // d += 'Buzz'; 232 | // c5 = 0; 233 | // } 234 | // if(d == ''){ 235 | // console.log(i); 236 | // }else{ 237 | // console.log(d); 238 | // } 239 | // } 240 | 241 | // CONTRIBUTED SOLUTION 242 | // for(let i = 1; i <= 100; i++) { 243 | // let printNum=true; 244 | // if(i % 3 === 0) 245 | // console.log('Fizz'); 246 | // if(i % 5 === 0) 247 | // console.log('Buzz'); 248 | // // if divisable by 3 then i%3 is zero, 249 | // // if divisable by 5 then i%5 is zero, 250 | // // if divisable by 3 or 5 then i%3*i%5 is zero then don't print 251 | // printNum=((i%3*i%5)!==0); 252 | // if(printNum) { 253 | // console.log(i); 254 | // } 255 | // } 256 | // CONTRIBUTED SOLUTION 257 | // for(var i = 1; i <= 100; i++) { 258 | // let output = ''; 259 | // if(i % 3 === 0) { 260 | // output += 'fizz'; 261 | // } 262 | // if(i % 5 === 0) { 263 | // output += 'buzz'; 264 | // } 265 | // console.log(output || i); 266 | // } 267 | // CONTRIBUTED SOLUTION 268 | // BY Ankita Patil 269 | // for (let i = 1; i <= 100; i++) { 270 | // let output = ''; 271 | // if (i % 3 === 0) { 272 | // output += 'Fizz'; 273 | // } 274 | // if (i % 5 === 0) { 275 | // output += 'Buzz'; 276 | // } 277 | // if (output === '') { 278 | // output = i; 279 | // } 280 | // console.log(output); 281 | // } 282 | } 283 | 284 | // Call Function 285 | const output = reverseString('hello'); 286 | 287 | console.log(output); 288 | ======= 289 | // CHALLENGE 1: REVERSE A STRING 290 | // Return a string in reverse 291 | // ex. reverseString('hello') === 'olleh' 292 | 293 | function reverseString(str) { 294 | // CONTRIBUTED SOLUTION 295 | // let arr = [...str]; 296 | // let newArr = arr.map((_, i, arr) => { 297 | // return arr[arr.length - 1 - i]; 298 | // }); 299 | // return newArr.join(""); 300 | // CONTRIBUTED SOLUTION 301 | // BY Ankita Patil 302 | // return str.split("").reduce((reversedString, character) => character + reversedString, ""); 303 | // CONTRIBUTED SOLUTION 304 | // BY James Robinson 305 | // if (str.length === 1) { 306 | // return str[0]; 307 | // } else { 308 | // return reverseString(str.slice(1)) + str[0]; 309 | // } 310 | } 311 | 312 | // CHALLENGE 2: VALIDATE A PALINDROME 313 | // Return true if palindrome and false if not 314 | // ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false 315 | 316 | function isPalindrome(str) { 317 | // CONTRIBUTED SOLUTION 318 | // let isPali = true; 319 | // let caseStr = str.toLowerCase() 320 | // let arr = caseStr.split('') 321 | // for(let i = 0; i < arr.length; i++){ 322 | // if(arr[i] !== arr[arr.length -1 -i]){ 323 | // isPali = false; 324 | // break; 325 | // } 326 | // } 327 | // return isPali 328 | } 329 | 330 | // CHALLENGE 3: REVERSE AN INTEGER 331 | // Return an integer in reverse 332 | // ex. reverseInt(521) === 125 333 | 334 | function reverseInt(int) {} 335 | 336 | // CHALLENGE 4: CAPITALIZE LETTERS 337 | // Return a string with the first letter of every word capitalized 338 | // ex. capitalizeLetters('i love javascript') === 'I Love Javascript' 339 | function capitalizeLetters(str) {} 340 | 341 | // CHALLENGE 5: MAX CHARACTER 342 | // Return the character that is most common in a string 343 | // ex. maxCharacter('javascript') == 'a' 344 | function maxCharacter(str) { 345 | // CONTRIBUTED SOLUTION 346 | // Here is my version of the algorithm. 347 | // I put the string into an array and I sort it. 348 | // Then I loop through the array keeping track of the count for each character. 349 | // I keep track of the "current character" and the "max character" with objects. 350 | // const sortedStr = str.split('').sort(); 351 | // const maxChar = {char: '', count: 0}; 352 | // const currentChar = {char: '', count: 0}; 353 | // sortedStr.forEach(char => { 354 | // if(currentChar.char === char){ 355 | // currentChar.count++; 356 | // } else { 357 | // if(maxChar.count < currentChar.count){ 358 | // maxChar.char = currentChar.char; 359 | // maxChar.count = currentChar.count; 360 | // } 361 | // currentChar.char = char; 362 | // currentChar.count = 0; 363 | // } 364 | // }); 365 | // return maxChar.char; 366 | 367 | 368 | // CONTRIBUTED SOLUTION 369 | // BY Romain Guilloteau 370 | // let letters = {}; 371 | // str 372 | // .toLowerCase() 373 | // .split('') 374 | // .forEach(l => (letters[l] = letters[l] + 1 || 1)); 375 | // const sortedLetters = Object.entries(letters) 376 | // .sort((a, b) => a[1] - b[1]) 377 | // .map(a => a[0]); 378 | // return sortedLetters[sortedLetters.length - 1]; 379 | // BY Bedirhan Dincer 380 | // const charMap = {}; 381 | // str 382 | // .split('') 383 | // .forEach(char => (charMap[char] = charMap[char] ? charMap[char] + 1 : 1)); 384 | // return Math.max(...Object.keys(charMap).map(char => charMap[char])); 385 | } 386 | 387 | // CHALLENGE 6: FIZZBUZZ 388 | // Write a program that prints all the numbers from 1 to 100. For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". 389 | function fizzBuzz() { 390 | // CONTRIBUTED SOLUTION 391 | // for(let i = 1; i <= 100; i++) { 392 | // let printNum=true; 393 | // if(i % 3 === 0) 394 | // console.log('Fizz'); 395 | // if(i % 5 === 0) 396 | // console.log('Buzz'); 397 | // // if divisable by 3 then i%3 is zero, 398 | // // if divisable by 5 then i%5 is zero, 399 | // // if divisable by 3 or 5 then i%3*i%5 is zero then don't print 400 | // printNum=((i%3*i%5)!==0); 401 | // if(printNum) { 402 | // console.log(i); 403 | // } 404 | // } 405 | // CONTRIBUTED SOLUTION 406 | // for(var i = 1; i <= 100; i++) { 407 | // let output = ''; 408 | // if(i % 3 === 0) { 409 | // output += 'fizz'; 410 | // } 411 | // if(i % 5 === 0) { 412 | // output += 'buzz'; 413 | // } 414 | // console.log(output || i); 415 | // } 416 | // CONTRIBUTED SOLUTION 417 | // BY Ankita Patil 418 | // for (let i = 1; i <= 100; i++) { 419 | // let output = ''; 420 | // if (i % 3 === 0) { 421 | // output += 'Fizz'; 422 | // } 423 | // if (i % 5 === 0) { 424 | // output += 'Buzz'; 425 | // } 426 | // if (output === '') { 427 | // output = i; 428 | // } 429 | // console.log(output); 430 | // } 431 | // CONTRIBUTED SOLUTION 432 | // BY Romain Guilloteau 433 | // for (let i = 1; i <= 100; i++) { 434 | // console.log([ 435 | // (i % 3 === 0 ? 'Fizz' : ''), 436 | // (i % 5 === 0 ? 'Buzz' : ''), 437 | // ].join('') || i); 438 | // } 439 | 440 | // CONTRIBUTED SOLUTION 441 | // By Ashish S 442 | // for (let i = 1; i < 101; i++) { 443 | // let a = i % 3 ? (i % 5 ? i : 'Buzz') : i % 5 ? 'Fizz' : 'FizzBuzz'; 444 | // console.log(a); 445 | // } 446 | 447 | // CONTRIBUTED SOLUTION 448 | // By Gamze Erol 449 | // for(i = 1; i<=100; i++) { 450 | // if (i % 3 === 0 && i % 5 === 0) console.log('FizzBuzz'); 451 | // else if (i % 3 === 0) console.log('Fizz'); 452 | // else if (i % 5 === 0) console.log('Buzz'); 453 | // else console.log(i); 454 | // } 455 | 456 | // CONTRIBUTED SOLUTION 457 | // By Lalit Kumar 458 | // for (let i = 1; i <= 100; i++) { 459 | // const output = (i % 3 === 0 ? "Fizz" : "") + (i % 5 === 0 ? "Buzz" : ""); 460 | // console.log(output || i); 461 | // } 462 | 463 | } 464 | 465 | // Call Function 466 | const output = reverseString('hello'); 467 | 468 | console.log(output); 469 | >>>>>>> e8090fea1d9fe2c208a8f8ce4cc46be9f287b108 470 | -------------------------------------------------------------------------------- /session1/index_finished.js: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | // CHALLENGE 1: REVERSE A STRING 3 | // Return a string in reverse 4 | // ex. reverseString('hello') === 'olleh' 5 | 6 | function reverseString(str) { 7 | // return str 8 | // .split('') 9 | // .reverse() 10 | // .join(''); 11 | 12 | ////////////////////////// 13 | 14 | // let revString = ''; 15 | // for(let i = str.length - 1; i >= 0; i--) { 16 | // revString = revString + str[i]; 17 | // } 18 | // return revString; 19 | 20 | ///////////////////////// 21 | 22 | // let revString = ''; 23 | // for(let i = 0; i <= str.length - 1; i++) { 24 | // revString = str[i] + revString; 25 | // } 26 | // return revString; 27 | 28 | //////////////////////// 29 | 30 | // let revString = ''; 31 | // for(let char of str) { 32 | // revString = char + revString; 33 | // } 34 | // return revString; 35 | 36 | /////////////////////// 37 | 38 | // let revString = ''; 39 | // str.split('').forEach(char => revString = char + revString); 40 | // return revString; 41 | 42 | ////////////////////// 43 | 44 | 45 | //let arr = [...str]; 46 | //let newArr = arr.map((_, i, arr) => { 47 | // return arr[arr.length - 1 - i]; 48 | // }); 49 | //return newArr.join(""); 50 | 51 | 52 | 53 | return str.split('').reduce((revString, char) => char + revString, ''); 54 | } 55 | 56 | 57 | // Reversing a string without using built-in function 58 | // Contributed by - Ankita Patil 59 | 60 | function reverseString(str) { 61 | let reversedString = ""; 62 | 63 | for(character of str) { 64 | reversedString = character + reversedString; 65 | } 66 | 67 | return reversedString; 68 | } 69 | 70 | // Another solution to reverse a string 71 | // Contributed by Ankita Patil 72 | 73 | function reverseString(str) { 74 | return str.split("").reduce((reversedString, character) => character + reversedString, ""); 75 | } 76 | 77 | // CHALLENGE 2: VALIDATE A PALINDROME 78 | // Return true if palindrome and false if not 79 | // ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false 80 | 81 | function isPalindrome(str) { 82 | const revString = str.split('').reverse().join(''); 83 | 84 | return revString === str; 85 | 86 | /////////////////////// 87 | 88 | // let isPali = true; 89 | // let caseStr = str.toLowerCase() 90 | // let arr = caseStr.split('') 91 | // for(let i = 0; i < arr.length; i++){ 92 | // if(arr[i] !== arr[arr.length -1 -i]){ 93 | // isPali = false; 94 | // break; 95 | // } 96 | // } 97 | // return isPali 98 | 99 | } 100 | 101 | 102 | 103 | // CHALLENGE 3: REVERSE AN INTEGER 104 | // Return an integer in reverse 105 | // ex. reverseInt(521) === 125 106 | 107 | function reverseInt(int) { 108 | const revString = int.toString().split('').reverse().join(''); 109 | 110 | return parseInt(revString) * Math.sign(int); 111 | } 112 | 113 | 114 | 115 | // CHALLENGE 4: CAPITALIZE LETTERS 116 | // Return a string with the first letter of every word capitalized 117 | // ex. capitalizeLetters('i love javascript') === 'I Love Javascript' 118 | function capitalizeLetters(str) { 119 | // const strArr = str.toLowerCase().split(' '); 120 | 121 | // for(let i = 0; i < strArr.length; i++) { 122 | // strArr[i] = strArr[i].substring(0, 1).toUpperCase() + strArr[i].substring(1); 123 | // } 124 | 125 | // return strArr.join(' '); 126 | 127 | ///////////////////////////// 128 | 129 | // return str 130 | // .toLowerCase() 131 | // .split(' ') 132 | // .map(word => word[0].toUpperCase() + word.substr(1)) 133 | // .join(' '); 134 | 135 | ///////////////////////////// 136 | 137 | return str.replace(/\b[a-z]/gi, function(char) { 138 | return char.toUpperCase(); 139 | }); 140 | } 141 | 142 | 143 | 144 | // CHALLENGE 5: MAX CHARACTER 145 | // Return the character that is most common in a string 146 | // ex. maxCharacter('javascript') == 'a' 147 | function maxCharacter(str) { 148 | const charMap = {}; 149 | let maxNum = 0; 150 | let maxChar = ''; 151 | 152 | str.split('').forEach(function(char) { 153 | if(charMap[char]) { 154 | charMap[char]++; 155 | } else { 156 | charMap[char] = 1; 157 | } 158 | }); 159 | 160 | for(let char in charMap) { 161 | // debugger; 162 | if(charMap[char] > maxNum) { 163 | maxNum = charMap[char]; 164 | maxChar = char; 165 | } 166 | } 167 | 168 | return maxChar; 169 | 170 | 171 | // Here is my version of the algorithm. 172 | // I put the string into an array and I sort it. 173 | // Then I loop through the array keeping track of the count for each character. 174 | // I keep track of the "current character" and the "max character" with objects. 175 | 176 | // const sortedStr = str.split('').sort(); 177 | // const maxChar = {char: '', count: 0}; 178 | // const currentChar = {char: '', count: 0}; 179 | 180 | // sortedStr.forEach(char => { 181 | // if(currentChar.char === char){ 182 | // currentChar.count++; 183 | // } else { 184 | // if(maxChar.count < currentChar.count){ 185 | // maxChar.char = currentChar.char; 186 | // maxChar.count = currentChar.count; 187 | // } 188 | // currentChar.char = char; 189 | // currentChar.count = 0; 190 | // } 191 | // }); 192 | 193 | // return maxChar.char; 194 | } 195 | 196 | 197 | 198 | // CHALLENGE 6: FIZZBUZZ 199 | // Write a program that prints all the numbers from 1 to 100. For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". 200 | function fizzBuzz() { 201 | // for(let i = 1; i <= 100; i++) { 202 | // if(i % 15 === 0) { 203 | // console.log('FizzBuzz'); 204 | // } else if(i % 3 === 0) { 205 | // console.log('Fizz'); 206 | // } else if(i % 5 === 0) { 207 | // console.log('Buzz'); 208 | // } else { 209 | // console.log(i); 210 | // } 211 | // } 212 | 213 | // for(let i = 1; i <= 100; i++) { 214 | // let printNum=true; 215 | // if(i % 3 === 0) 216 | // console.log('Fizz'); 217 | // if(i % 5 === 0) 218 | // console.log('Buzz'); 219 | // // if divisable by 3 then i%3 is zero, 220 | // // if divisable by 5 then i%5 is zero, 221 | // // if divisable by 3 or 5 then i%3*i%5 is zero then don't print 222 | // printNum=((i%3*i%5)!==0); 223 | // if(printNum) { 224 | // console.log(i); 225 | // } 226 | // } 227 | for(var i = 1; i <= 100; i++) { 228 | let output = ''; 229 | if(i % 3 === 0) { 230 | output += 'fizz'; 231 | } 232 | if(i % 5 === 0) { 233 | output += 'buzz'; 234 | } 235 | console.log(output || i); 236 | } 237 | } 238 | 239 | 240 | 241 | // Call Function 242 | const output = fizzBuzz(); 243 | 244 | console.log(output); 245 | 246 | 247 | // Solution 2 248 | // Author: Ankita Patil 249 | function fizzBuzz() { 250 | for (let i = 1; i <= 100; i++) { 251 | let output = ""; 252 | 253 | if(i % 3 === 0) { 254 | output += 'Fizz'; 255 | } 256 | 257 | if(i % 5 === 0) { 258 | output += 'Buzz'; 259 | } 260 | 261 | if(output === "") { 262 | output = i; 263 | } 264 | 265 | console.log(output); 266 | 267 | } 268 | } 269 | 270 | console.log(fizzBuzz()); 271 | ======= 272 | // CHALLENGE 1: REVERSE A STRING 273 | // Return a string in reverse 274 | // ex. reverseString('hello') === 'olleh' 275 | 276 | function reverseString(str) { 277 | // return str 278 | // .split('') 279 | // .reverse() 280 | // .join(''); 281 | 282 | ////////////////////////// 283 | 284 | // let revString = ''; 285 | // for(let i = str.length - 1; i >= 0; i--) { 286 | // revString = revString + str[i]; 287 | // } 288 | // return revString; 289 | 290 | ///////////////////////// 291 | 292 | // let revString = ''; 293 | // for(let i = 0; i <= str.length - 1; i++) { 294 | // revString = str[i] + revString; 295 | // } 296 | // return revString; 297 | 298 | //////////////////////// 299 | 300 | // let revString = ''; 301 | // for(let char of str) { 302 | // revString = char + revString; 303 | // } 304 | // return revString; 305 | 306 | /////////////////////// 307 | 308 | // let revString = ''; 309 | // str.split('').forEach(char => revString = char + revString); 310 | // return revString; 311 | 312 | ////////////////////// 313 | 314 | 315 | //let arr = [...str]; 316 | //let newArr = arr.map((_, i, arr) => { 317 | // return arr[arr.length - 1 - i]; 318 | // }); 319 | //return newArr.join(""); 320 | 321 | 322 | 323 | return str.split('').reduce((revString, char) => char + revString, ''); 324 | } 325 | 326 | 327 | // Reversing a string without using built-in function 328 | // Contributed by - Ankita Patil 329 | 330 | function reverseString(str) { 331 | let reversedString = ""; 332 | 333 | for(character of str) { 334 | reversedString = character + reversedString; 335 | } 336 | 337 | return reversedString; 338 | } 339 | 340 | // Another solution to reverse a string 341 | // Contributed by Ankita Patil 342 | 343 | function reverseString(str) { 344 | return str.split("").reduce((reversedString, character) => character + reversedString, ""); 345 | } 346 | 347 | // CHALLENGE 2: VALIDATE A PALINDROME 348 | // Return true if palindrome and false if not 349 | // ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false 350 | 351 | function isPalindrome(str) { 352 | const revString = str.split('').reverse().join(''); 353 | 354 | return revString === str; 355 | 356 | /////////////////////// 357 | 358 | // let isPali = true; 359 | // let caseStr = str.toLowerCase() 360 | // let arr = caseStr.split('') 361 | // for(let i = 0; i < arr.length; i++){ 362 | // if(arr[i] !== arr[arr.length -1 -i]){ 363 | // isPali = false; 364 | // break; 365 | // } 366 | // } 367 | // return isPali 368 | 369 | } 370 | 371 | // shortest one 372 | // function isPal(s) { 373 | // return s.split('').reverse().join('') === s; 374 | // } 375 | 376 | 377 | 378 | // CHALLENGE 3: REVERSE AN INTEGER 379 | // Return an integer in reverse 380 | // ex. reverseInt(521) === 125 381 | 382 | function reverseInt(int) { 383 | const revString = int.toString().split('').reverse().join(''); 384 | 385 | return parseInt(revString) * Math.sign(int); 386 | } 387 | 388 | 389 | 390 | // CHALLENGE 4: CAPITALIZE LETTERS 391 | // Return a string with the first letter of every word capitalized 392 | // ex. capitalizeLetters('i love javascript') === 'I Love Javascript' 393 | function capitalizeLetters(str) { 394 | // const strArr = str.toLowerCase().split(' '); 395 | 396 | // for(let i = 0; i < strArr.length; i++) { 397 | // strArr[i] = strArr[i].substring(0, 1).toUpperCase() + strArr[i].substring(1); 398 | // } 399 | 400 | // return strArr.join(' '); 401 | 402 | ///////////////////////////// 403 | 404 | // return str 405 | // .toLowerCase() 406 | // .split(' ') 407 | // .map(word => word[0].toUpperCase() + word.substr(1)) 408 | // .join(' '); 409 | 410 | ///////////////////////////// 411 | 412 | return str.replace(/\b[a-z]/gi, function(char) { 413 | return char.toUpperCase(); 414 | }); 415 | } 416 | 417 | 418 | 419 | // CHALLENGE 5: MAX CHARACTER 420 | // Return the character that is most common in a string 421 | // ex. maxCharacter('javascript') == 'a' 422 | function maxCharacter(str) { 423 | const charMap = {}; 424 | let maxNum = 0; 425 | let maxChar = ''; 426 | 427 | str.split('').forEach(function(char) { 428 | if(charMap[char]) { 429 | charMap[char]++; 430 | } else { 431 | charMap[char] = 1; 432 | } 433 | }); 434 | 435 | for(let char in charMap) { 436 | // debugger; 437 | if(charMap[char] > maxNum) { 438 | maxNum = charMap[char]; 439 | maxChar = char; 440 | } 441 | } 442 | 443 | return maxChar; 444 | 445 | 446 | // Here is my version of the algorithm. 447 | // I put the string into an array and I sort it. 448 | // Then I loop through the array keeping track of the count for each character. 449 | // I keep track of the "current character" and the "max character" with objects. 450 | 451 | // const sortedStr = str.split('').sort(); 452 | // const maxChar = {char: '', count: 0}; 453 | // const currentChar = {char: '', count: 0}; 454 | 455 | // sortedStr.forEach(char => { 456 | // if(currentChar.char === char){ 457 | // currentChar.count++; 458 | // } else { 459 | // if(maxChar.count < currentChar.count){ 460 | // maxChar.char = currentChar.char; 461 | // maxChar.count = currentChar.count; 462 | // } 463 | // currentChar.char = char; 464 | // currentChar.count = 0; 465 | // } 466 | // }); 467 | 468 | // return maxChar.char; 469 | } 470 | 471 | 472 | 473 | // CHALLENGE 6: FIZZBUZZ 474 | // Write a program that prints all the numbers from 1 to 100. For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". 475 | function fizzBuzz() { 476 | // for(let i = 1; i <= 100; i++) { 477 | // if(i % 15 === 0) { 478 | // console.log('FizzBuzz'); 479 | // } else if(i % 3 === 0) { 480 | // console.log('Fizz'); 481 | // } else if(i % 5 === 0) { 482 | // console.log('Buzz'); 483 | // } else { 484 | // console.log(i); 485 | // } 486 | // } 487 | 488 | // for(let i = 1; i <= 100; i++) { 489 | // let printNum=true; 490 | // if(i % 3 === 0) 491 | // console.log('Fizz'); 492 | // if(i % 5 === 0) 493 | // console.log('Buzz'); 494 | // // if divisable by 3 then i%3 is zero, 495 | // // if divisable by 5 then i%5 is zero, 496 | // // if divisable by 3 or 5 then i%3*i%5 is zero then don't print 497 | // printNum=((i%3*i%5)!==0); 498 | // if(printNum) { 499 | // console.log(i); 500 | // } 501 | // } 502 | for(var i = 1; i <= 100; i++) { 503 | let output = ''; 504 | if(i % 3 === 0) { 505 | output += 'fizz'; 506 | } 507 | if(i % 5 === 0) { 508 | output += 'buzz'; 509 | } 510 | console.log(output || i); 511 | } 512 | } 513 | 514 | 515 | 516 | // Call Function 517 | const output = fizzBuzz(); 518 | 519 | console.log(output); 520 | 521 | 522 | // Solution 2 523 | // Author: Ankita Patil 524 | function fizzBuzz() { 525 | for (let i = 1; i <= 100; i++) { 526 | let output = ""; 527 | 528 | if(i % 3 === 0) { 529 | output += 'Fizz'; 530 | } 531 | 532 | if(i % 5 === 0) { 533 | output += 'Buzz'; 534 | } 535 | 536 | if(output === "") { 537 | output = i; 538 | } 539 | 540 | console.log(output); 541 | 542 | } 543 | } 544 | 545 | console.log(fizzBuzz()); 546 | >>>>>>> e8090fea1d9fe2c208a8f8ce4cc46be9f287b108 547 | -------------------------------------------------------------------------------- /session1/index_start.js: -------------------------------------------------------------------------------- 1 | // CHALLENGE 1: REVERSE A STRING 2 | // Return a string in reverse 3 | // ex. reverseString('hello') === 'olleh' 4 | 5 | function reverseString(str) { 6 | return 'Let\'s Start' 7 | } 8 | 9 | 10 | 11 | // CHALLENGE 2: VALIDATE A PALINDROME 12 | // Return true if palindrome and false if not 13 | // ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false 14 | 15 | function isPalindrome(str) {} 16 | 17 | 18 | 19 | // CHALLENGE 3: REVERSE AN INTEGER 20 | // Return an integer in reverse 21 | // ex. reverseInt(521) === 125 22 | 23 | function reverseInt(int) {} 24 | 25 | 26 | 27 | // CHALLENGE 4: CAPITALIZE LETTERS 28 | // Return a string with the first letter of every word capitalized 29 | // ex. capitalizeLetters('i love javascript') === 'I Love Javascript' 30 | function capitalizeLetters(str) {} 31 | 32 | 33 | 34 | // CHALLENGE 5: MAX CHARACTER 35 | // Return the character that is most common in a string 36 | // ex. maxCharacter('javascript') == 'a' 37 | function maxCharacter(str) {} 38 | 39 | 40 | 41 | // CHALLENGE 6: FIZZBUZZ 42 | // Write a program that prints all the numbers from 1 to 100. For multiples of 3, instead of the number, print "Fizz", for multiples of 5 print "Buzz". For numbers which are multiples of both 3 and 5, print "FizzBuzz". 43 | function fizzBuzz() {} 44 | 45 | 46 | 47 | // Call Function 48 | const output = reverseString('hello'); 49 | 50 | console.log(output); 51 | -------------------------------------------------------------------------------- /session2/index_extra.js: -------------------------------------------------------------------------------- 1 | ////// INSTRUCTIONS ///// 2 | // ADD YOUR SOLUTIONS HERE 3 | // COMMENT YOUR SOLUTION OUT BEFORE YOU MAKE A PULL REQUEST 4 | // ADD "SOLUTION BY [YOUR NAME]" 5 | 6 | // CHALLENGE 1: LONGEST WORD 7 | // Return the longest word of a string 8 | // If more than one longest word, put into an array 9 | // ex. longestWord('Hello, my name is Brad') === 'hello' 10 | // ex. longestWord('Hello there, my name is Brad') === ['hello', 'there'] 11 | 12 | function longestWord(sen) {} 13 | 14 | // CHALLENGE 2: ARRAY CHUNKING 15 | // Split an array into chunked arrays of a specific length 16 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 3) === [[1, 2, 3],[4, 5, 6],[7]] 17 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 2) === [[1, 2],[3, 4],[5, 6],[7]] 18 | 19 | function chunkArray(arr, len) { 20 | // SOLUTION BY Jawad Mohammad 21 | // return arr.reduce((acc, cv, ci, array) => { 22 | // if (ci % len === 0 ) { 23 | // acc.push(array.slice(ci, ci+len)); 24 | // } 25 | // return acc; 26 | // }, []); 27 | 28 | // ----- SOLUTION BY Jon Bodnar ----- // 29 | // const chunked = []; 30 | // while (arr.length) { 31 | // chunked.push(arr.splice(0, len)); 32 | // } 33 | // return chunked; 34 | // ----- END ----- // 35 | } 36 | 37 | // CHALLENGE 3: FLATTEN ARRAY 38 | // Take an array of arrays and flatten to a single array 39 | // ex. [[1, 2], [3, 4], [5, 6], [7]] = [1, 2, 3, 4, 5, 6, 7] 40 | 41 | function flattenArray(arrays) { 42 | // SOLUTION BY Jawad Mohammad 43 | // return arrays.reduce((acc, cv) => { 44 | // acc.push(...cv); 45 | // return acc; 46 | // }, []); 47 | } 48 | 49 | // CHALLENGE 4: ANAGRAM 50 | // Return true if anagram and false if not 51 | // ex. 'elbow' === 'below' 52 | // ex. 'Dormitory' === 'dirty room##' 53 | 54 | function isAnagram(str1, str2) {} 55 | 56 | // CHALLENGE 5: LETTER CHANGES 57 | // Change every letter of the string to the one that follows it and capitalize the vowels 58 | // Z should turn to A 59 | // ex. 'hello there' === 'Ifmmp UIfsf' 60 | 61 | function letterChanges(str) {} 62 | 63 | // Call Function 64 | const output = longestWord("Hello there, my name is Brad"); 65 | 66 | console.log(output); 67 | ////// INSTRUCTIONS ///// 68 | // ADD YOUR SOLUTIONS HERE 69 | // COMMENT YOUR SOLUTION OUT BEFORE YOU MAKE A PULL REQUEST 70 | // ADD "SOLUTION BY [YOUR NAME]" 71 | 72 | // CHALLENGE 1: LONGEST WORD 73 | // Return the longest word of a string 74 | // If more than one longest word, put into an array 75 | // ex. longestWord('Hello, my name is Brad') === 'hello' 76 | // ex. longestWord('Hello there, my name is Brad') === ['hello', 'there'] 77 | 78 | function longestWord(sen) { 79 | // SOLUTION BY @aweebit 80 | // 81 | // let maxLetters = 0; 82 | // let outputArr = []; 83 | // sen.toLowerCase() 84 | // .match(/\w+/g) 85 | // .forEach(word => { 86 | // if (!(word.length < maxLetters)) { 87 | // if (word.length > maxLetters) { 88 | // maxLetters = word.length; 89 | // outputArr = []; 90 | // } 91 | // outputArr.push(word); 92 | // } 93 | // }); 94 | // return outputArr.length > 1 ? outputArr : outputArr[0]; 95 | } 96 | 97 | // CHALLENGE 2: ARRAY CHUNKING 98 | // Split an array into chunked arrays of a specific length 99 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 3) === [[1, 2, 3],[4, 5, 6],[7]] 100 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 2) === [[1, 2],[3, 4],[5, 6],[7]] 101 | 102 | function chunkArray(arr, len) { 103 | // SOLUTION BY @mattmccherry 104 | // let i=0, output = [], currArr = []; 105 | // 106 | // arr.forEach(element => { 107 | // currArr.push(element); 108 | // i++; 109 | // if (i%len === 0 || i === arr.length) { 110 | // output.push(currArr); 111 | // currArr = []; 112 | // } 113 | // }); 114 | // return output; 115 | // SOLUTION ADDED BY @mattmccherry from https://30secondsofcode.org/#chunk 116 | // 117 | // return Array.from({ length: Math.ceil(arr.length / len) }, (v, i) => 118 | // arr.slice(i * len, i * len + len) 119 | // ); 120 | 121 | // SOLUTION BY @jerryakpera 122 | // const chunks = [] 123 | 124 | // while (arr.length >= len) { 125 | // chunks.push(arr.splice(0, len)) 126 | // } 127 | // chunks.push(arr.splice(0)) 128 | // return chunks 129 | } 130 | 131 | // CHALLENGE 3: FLATTEN ARRAY 132 | // Take an array of arrays and flatten to a single array 133 | // ex. [[1, 2], [3, 4], [5, 6], [7]] = [1, 2, 3, 4, 5, 6, 7] 134 | 135 | function flattenArray(arrays) { 136 | // SOLUTION BY colebearski 137 | // 138 | // return arrays.flat(); 139 | // 140 | // SOLUTION BY @mattmccherry 141 | // 142 | // return arrays.reduce((arr, nums) => [...arr, ...nums], []); 143 | // SOLUTION BY patryklkakol: 144 | // return arrays 145 | // .join() 146 | // .split(',') 147 | // .map(a => +a || a); 148 | } 149 | 150 | // CHALLENGE 4: ANAGRAM 151 | // Return true if anagram and false if not 152 | // ex. 'elbow' === 'below' 153 | // ex. 'Dormitory' === 'dirty room##' 154 | 155 | function isAnagram(str1, str2) {} 156 | 157 | // CHALLENGE 5: LETTER CHANGES 158 | // Change every letter of the string to the one that follows it and capitalize the vowels 159 | // Z should turn to A 160 | // ex. 'hello there' === 'Ifmmp UIfsf' 161 | 162 | function letterChanges(str) { 163 | // SOLUTION BY @aweebit 164 | // // Practically the same, but within one statement and with a few enhancements 165 | // return str.toLowerCase().replace(/[a-z]/g, char => ( 166 | // char == 'z' ? 'A' : String.fromCharCode(char.charCodeAt() + 1) 167 | // )).replace(/[aeiou]/g, vowel => vowel.toUpperCase()); 168 | 169 | // SOLUTION BY @m0nm 170 | // const alphabet = 'AbcdEfghIjklmnOpqrstUvwxyz'.split('') /* too lazy to split it myself */ 171 | // const charArr = str.toLowerCase().replace(/[^a-z]/g, "").split('') 172 | 173 | // charArr.forEach((char, charIndex) => { 174 | 175 | // if (char === 'z') { 176 | // charArr[charIndex] = "A" 177 | // } 178 | 179 | // else { 180 | // const index = alphabet.findIndex(alp => char === alp) 181 | 182 | // charArr[charIndex] = alphabet[index + 1] 183 | // } 184 | // }) 185 | 186 | // const newStr = charArr.join('').replace(/([aeiou])/g, match => match.toUpperCase()) 187 | 188 | // return charArr.join('') 189 | } 190 | 191 | // Call Function 192 | const output = longestWord("Hello there, my name is Brad"); 193 | 194 | console.log(output); 195 | -------------------------------------------------------------------------------- /session2/index_finished.js: -------------------------------------------------------------------------------- 1 | // CHALLENGE 1: LONGEST WORD 2 | // Return the longest word of a string 3 | // If more than one longest word, put into an array 4 | // ex. longestWord('Hello, my name is Brad') === 'hello' 5 | // ex. longestWord('Hello there, my name is Brad') === ['hello', 'there'] 6 | 7 | function longestWord(sen) { 8 | // Create filtered array 9 | const wordArr = sen.toLowerCase().match(/[a-z0-9]+/g); 10 | 11 | // Sort by length 12 | const sorted = wordArr.sort((a, b) => b.length - a.length); 13 | 14 | // If multiple words, put into array 15 | const longestWordArr = sorted.filter( 16 | word => word.length === sorted[0].length 17 | ); 18 | 19 | // Check if more than one array val 20 | if (longestWordArr.length === 1) { 21 | // Return the word 22 | return longestWordArr[0]; 23 | } else { 24 | return longestWordArr; 25 | } 26 | } 27 | 28 | // CHALLENGE 2: ARRAY CHUNKING 29 | // Split an array into chunked arrays of a specific length 30 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 3) === [[1, 2, 3],[4, 5, 6],[7]] 31 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 2) === [[1, 2],[3, 4],[5, 6],[7]] 32 | 33 | function chunkArray(arr, len) { 34 | // SOLUTION 1 35 | 36 | // // Init chunked arr 37 | // const chunkedArr = []; 38 | // // Set index 39 | // let i = 0; 40 | 41 | // // Loop while index is less than the array length 42 | // while (i < arr.length) { 43 | // // Slice out from the index to the index + the chunk length nd push on to the chunked array 44 | // chunkedArr.push(arr.slice(i, i + len)); 45 | // // Increment by chunk length 46 | // i += len; 47 | // } 48 | 49 | // return chunkedArr; 50 | 51 | // SOLUTION 2 52 | 53 | // Init chunked arr 54 | const chunkedArr = []; 55 | 56 | // Loop through arr 57 | arr.forEach(val => { 58 | // Get last element 59 | const last = chunkedArr[chunkedArr.length - 1]; 60 | 61 | // Check if last and if last length is equal to the chunk len 62 | if (!last || last.length === len) { 63 | chunkedArr.push([val]); 64 | } else { 65 | last.push(val); 66 | } 67 | }); 68 | 69 | return chunkedArr; 70 | } 71 | 72 | // CHALLENGE 3: FLATTEN ARRAY 73 | // Take an array of arrays and flatten to a single array 74 | // ex. [[1, 2], [3, 4], [5, 6], [7]] = [1, 2, 3, 4, 5, 6, 7] 75 | 76 | function flattenArray(arrays) { 77 | // SOLUTION 1 78 | // return arrays.reduce((a, b) => a.concat(b)); 79 | // SOLUTION 2 80 | // return [].concat.apply([], arrays); 81 | // SOLUTION 3 82 | // return [].concat(...arrays); 83 | // SOLUTION 4 84 | return arrays.flat(Infinity); 85 | } 86 | 87 | //____________________________@missx7________________________ 88 | 89 | function flatenArray(arr){ 90 | newArr = [] 91 | for( let i = 0 ; i < arr.length ; i++){ 92 | for(let j = 0 ; j < arr[i].length ; j++){ 93 | newArr.push(arr[i][j]) 94 | } 95 | } 96 | return newArr 97 | } 98 | //____________#______________@missx7____________#___________ 99 | 100 | // CHALLENGE 4: ANAGRAM 101 | // Return true if anagram and false if not 102 | // ex. 'elbow' === 'below' 103 | // ex. 'Dormitory' === 'dirty room##' 104 | 105 | function isAnagram(str1, str2) { 106 | return formatStr(str1) === formatStr(str2); 107 | } 108 | 109 | // Helper function 110 | function formatStr(str) { 111 | return str 112 | .replace(/[^\w]/g, '') 113 | .toLowerCase() 114 | .split('') 115 | .sort() 116 | .join(''); 117 | } 118 | 119 | //___________________________@missx7________________________ 120 | 121 | function isAnagramx (str1,str2){ 122 | const arr1 = str1.split('').sort() 123 | const arr2 = str2.split('').sort() 124 | return arr1.every((value,index) => value === arr2[index]) 125 | } 126 | 127 | const isAnagramx = (str1,str2) => str1.split('').sort().every((value,index) => value === str2.split('').sort()[index]) 128 | 129 | //____________#______________@missx7____________#___________ 130 | 131 | // CHALLENGE 5: LETTER CHANGES 132 | // Change every letter of the string to the one that follows it and capitalize the vowels 133 | // Z should turn to A 134 | // ex. 'hello there' === 'Ifmmp UIfsf' 135 | 136 | function letterChanges(str) { 137 | let newStr = str.toLowerCase().replace(/[a-z]/gi, char => { 138 | if (char === 'z' || char === 'Z') { 139 | return 'a'; 140 | } else { 141 | return String.fromCharCode(char.charCodeAt() + 1); 142 | } 143 | }); 144 | 145 | newStr = newStr.replace(/a|e|i|o|u/gi, vowel => vowel.toUpperCase()); 146 | 147 | return newStr; 148 | } 149 | 150 | // Call Function 151 | const output = letterChanges('Hello There'); 152 | 153 | console.log(output); 154 | -------------------------------------------------------------------------------- /session2/index_start.js: -------------------------------------------------------------------------------- 1 | // CHALLENGE 1: LONGEST WORD 2 | // Return the longest word of a string 3 | // ex. longestWord('Hi there, my name is Brad') === 'there,' 4 | 5 | function longestWord(sen) { 6 | // SOLUTION 1 - Return a single longest word 7 | // SOLUTION 2 - Return an array and include multiple words if they have the same length 8 | // SOLUTION 3 - Only return an array if multiple words, otherwise return a string 9 | } 10 | 11 | // CHALLENGE 2: ARRAY CHUNKING 12 | // Split an array into chunked arrays of a specific length 13 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 3) === [[1, 2, 3],[4, 5, 6],[7]] 14 | // ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 2) === [[1, 2],[3, 4],[5, 6],[7]] 15 | 16 | function chunkArray(arr, len) {} 17 | 18 | // CHALLENGE 3: FLATTEN ARRAY 19 | // Take an array of arrays and flatten to a single array 20 | // ex. [[1, 2], [3, 4], [5, 6], [7]] = [1, 2, 3, 4, 5, 6, 7] 21 | 22 | function flattenArray(arrays) {} 23 | 24 | // CHALLENGE 4: ANAGRAM 25 | // Return true if anagram and false if not 26 | // ex. 'elbow' === 'below' 27 | // ex. 'Dormitory' === 'dirty room##' 28 | 29 | function isAnagram(str1, str2) {} 30 | 31 | // CHALLENGE 5: LETTER CHANGES 32 | // Change every letter of the string to the one that follows it and capitalize the vowels 33 | // Z should turn to A 34 | // ex. 'hello there' === 'Ifmmp UIfsf' 35 | 36 | function letterChanges(str) {} 37 | 38 | // Call Function 39 | const output = longestWord('Hello, my name is Brad'); 40 | 41 | console.log(output); 42 | -------------------------------------------------------------------------------- /session3/index_extra.js: -------------------------------------------------------------------------------- 1 | ////// INSTRUCTIONS ///// 2 | // ADD YOUR SOLUTIONS HERE 3 | // COMMENT YOUR SOLUTION OUT BEFORE YOU MAKE A PULL REQUEST 4 | // ADD "SOLUTION BY [YOUR NAME]" 5 | 6 | // CHALLENGE 1: ADD ALL NUMBERS 7 | // Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS 8 | // ex. addAll(2,5,6,7) === 20 9 | 10 | function addAll() {} 11 | 12 | // __________________@missx________________ 13 | 14 | const addAll = (...args) => args.reduce((a,b)=> a+b) 15 | console.log( addAll(2,5,6,7)) 16 | 17 | 18 | // CHALLENGE 2: SUM ALL PRIMES 19 | // Pass in a number to loop up to and add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself 20 | // ex. sumAllPrimes(10) == 17 21 | 22 | function sumAllPrimes() {} 23 | 24 | // __________________@missx________________ 25 | const isPrime = num => { 26 | for(let i = 2; i < num; i++) 27 | if(num % i === 0) return false; 28 | return num > 1; 29 | } 30 | 31 | function sumAllPrimes(num) { 32 | var sumNum = 0 33 | for(let i = 2 ; i < num ; i++){ 34 | if(isPrime(i)){ 35 | sumNum += i 36 | } 37 | 38 | } 39 | return sumNum 40 | } 41 | console.log(sumAllPrimes(10)) 42 | 43 | // CHALLENGE 3: SEEK & DESTROY 44 | // Remove from the array whatever is in the following arguments. Return the leftover numbers in an array 45 | // ex. seekAndDestroy([2, 3, 4, 6, 6, 'hello'], 2, 6) == [3, 4, 'hello'] 46 | 47 | function seekAndDestroy() {} 48 | 49 | // CHALLENGE 4: SORT BY HEIGHT 50 | // Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. 51 | // ex. 52 | // a = [-1, 150, 190, 170, -1, -1, 160, 180] 53 | // sortByHeight(a) == [-1, 150, 160, 170, -1, -1, 180, 190] 54 | 55 | function sortByHeight() { 56 | // SOLUTION BY Smollet777 57 | // filtering out and sorting all "humans" 58 | // const humans = a.filter(v => v !== -1).sort((a, b) => a - b) 59 | // put sorted "humans" back 60 | // return a.map(v => v !== -1? humans.shift() : -1) 61 | } 62 | 63 | // CHALLENGE 5: MISSING LETTERS 64 | // Find the missing letter in the passed letter range and return it. If all letters are present, return undefined 65 | // ex. 66 | // missingLetters("abce") == "d" 67 | // missingLetters("abcdefghjklmno") == "i" 68 | // missingLetters("abcdefghijklmnopqrstuvwxyz") == undefined 69 | 70 | function missingLetters(str) { 71 | const start = 'a'.charCodeAt() 72 | const end = 'z'.charCodeAt() 73 | 74 | // Generate the Valid Letters according to the test 75 | let validLetters = []; 76 | for (i = start; i <= end; i++) { 77 | validLetters.push(String.fromCharCode(i)) 78 | } 79 | 80 | // arrayfy the given parameter letters 81 | const letters = str.split(''); 82 | // initialize an empty array of the missing letters 83 | let missing = []; 84 | 85 | // traverse through the validLetters 86 | validLetters.forEach(letter => { 87 | // if a valid letter is not in the item, 88 | // push it to the `missing` variable 89 | if (!letters.includes(letter)) { 90 | missing.push(letter); 91 | } 92 | }); 93 | 94 | // Checks if missing filled, else return undefined 95 | return missing.length ? missing : undefined; 96 | } 97 | 98 | const letters = 'abcdefghijkmnoprstuvwxyz' 99 | console.log('Missing: ' + missingLetters(letters)) // `Missing: l, q`; 100 | 101 | //________________________@missx______________________ 102 | function missingLetters(str) { 103 | arr = str.split('') 104 | for(let i = 0 ; i < arr.length ; i++){ 105 | let letter = arr[i] 106 | let nextLetter = String.fromCharCode(letter.charCodeAt()+1) 107 | if(letter == 'z'){ 108 | return undefined 109 | } else if(nextLetter !== arr[i + 1]){ 110 | return nextLetter 111 | } 112 | } 113 | } 114 | console.log(missingLetters("abcdefghijklmnopqrstuvwxyz")) 115 | 116 | 117 | // CHALLENGE 6: EVEN & ODD SUMS 118 | // Take in an array and return an array of the sums of even and odd numbers 119 | // ex. 120 | // evenOddSums([50, 60, 60, 45, 71]) == [170, 116] 121 | 122 | function evenOddSums() {} 123 | 124 | //_____________________ @missx________________ 125 | function evenOddSums(arr) { 126 | let sumEven = 0 ; 127 | let SumOdd = 0 ; 128 | for(i of arr){ 129 | i % 2 == 0 ? SumOdd +=i : sumEven +=i 130 | } 131 | return [SumOdd , sumEven] 132 | } 133 | console.log(evenOddSums([50, 60, 60, 45, 71])) 134 | 135 | -------------------------------------------------------------------------------- /session3/index_finished.js: -------------------------------------------------------------------------------- 1 | // CHALLENGE 1: ADD ALL NUMBERS 2 | // Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS 3 | // ex. addAll(2,5,6,7) === 20 4 | 5 | // Solution 1: ES5 arguments & for loop 6 | function addAll() { 7 | var args = Array.prototype.slice.call(arguments); 8 | var total = 0; 9 | for (let i = 0; i < args.length; i++) { 10 | total += args[i]; 11 | } 12 | return total; 13 | } 14 | 15 | // Solution 2: ...rest & reduce/forEach 16 | function addAll(...numbers) { 17 | return numbers.reduce((acc, cur) => acc + cur); 18 | } 19 | 20 | // CHALLENGE 2: SUM ALL PRIMES 21 | // Pass in a number to loop up to and add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself 22 | // ex. sumAllPrimes(10) == 17 23 | 24 | function sumAllPrimes(num) { 25 | let total = 0; 26 | 27 | function checkForPrime(i) { 28 | for (let j = 2; j < i; j++) { 29 | if (i % j === 0) { 30 | return false; 31 | } 32 | } 33 | return true; 34 | } 35 | 36 | for (let i = 2; i <= num; i++) { 37 | if (checkForPrime(i)) { 38 | total += i; 39 | } 40 | } 41 | return total; 42 | } 43 | 44 | // CHALLENGE 3: SEEK & DESTROY 45 | // Remove from the array whatever is in the following arguments. Return the leftover values in an array 46 | // ex. seekAndDestroy([2, 3, 4, 6, 6, 'hello'], 2, 6) == [3, 4, 'hello'] 47 | 48 | // Solution 1: arguments, indexOf, filter 49 | function seekAndDestroy(arr) { 50 | const args = Array.from(arguments); 51 | 52 | function filterArr(arr) { 53 | // Return true if NOT in array 54 | return args.indexOf(arr) === -1; 55 | } 56 | 57 | return arr.filter(filterArr); 58 | } 59 | 60 | // Solution 2: ...rest, filter & includes 61 | function seekAndDestroy(arr, ...rest) { 62 | return arr.filter(val => !rest.includes(val)); 63 | } 64 | 65 | // CHALLENGE 4: SORT BY HEIGHT 66 | // Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. 67 | // ex. 68 | // a = [-1, 150, 190, 170, -1, -1, 160, 180] 69 | // sortByHeight(a) == [-1, 150, 160, 170, -1, -1, 180, 190] 70 | 71 | function sortByHeight(a) { 72 | const arr1 = []; 73 | const arr2 = []; 74 | 75 | a.forEach((val, i) => (val === -1 ? arr1.push(i) : arr2.push(val))); 76 | 77 | const sortArr = arr2.sort((a, b) => a - b); 78 | 79 | arr1.forEach((val, i) => sortArr.splice(arr1[i], 0, -1)); 80 | 81 | return sortArr; 82 | } 83 | 84 | // CHALLENGE 5: MISSING LETTERS 85 | // Find the missing letter in the passed letter range and return it. If all letters are present, return undefined 86 | // ex. 87 | // missingLetters("abce") == "d" 88 | // missingLetters("abcdefghjklmno") == "i" 89 | // missingLetters("abcdefghijklmnopqrstuvwxyz") == undefined 90 | 91 | function missingLetters(str) { 92 | let compare = str.charCodeAt(0); 93 | let missing; 94 | 95 | str.split('').map((char, i) => { 96 | if (str.charCodeAt(i) == compare) { 97 | ++compare; 98 | } else { 99 | missing = String.fromCharCode(compare); 100 | } 101 | }); 102 | 103 | return missing; 104 | } 105 | 106 | // CHALLENGE 6: EVEN & ODD SUMS 107 | // Take in an array and return an array of the sums of even and odd numbers 108 | // ex. 109 | // evenOddSums([50, 60, 60, 45, 71]) == [170, 116] 110 | 111 | function evenOddSums(arr) { 112 | let evenSum = 0; 113 | let oddSum = 0; 114 | 115 | arr.forEach(num => (num % 2 === 0 ? (evenSum += num) : (oddSum += num))); 116 | 117 | return [evenSum, oddSum]; 118 | } 119 | -------------------------------------------------------------------------------- /session3/index_start.js: -------------------------------------------------------------------------------- 1 | // CHALLENGE 1: ADD ALL NUMBERS 2 | // Return a sum of all parameters entered regardless of the amount of numbers - NO ARRAYS 3 | // ex. addAll(2,5,6,7) === 20 4 | 5 | function addAll() {} 6 | 7 | // CHALLENGE 2: SUM ALL PRIMES 8 | // Pass in a number to loop up to and add all of the prime numbers. A prime number is a whole number greater than 1 whose only factors are 1 and itself 9 | // ex. sumAllPrimes(10) == 17 10 | 11 | function sumAllPrimes() {} 12 | 13 | // CHALLENGE 3: SEEK & DESTROY 14 | // Remove from the array whatever is in the following arguments. Return the leftover numbers in an array 15 | // ex. seekAndDestroy([2, 3, 4, 6, 6, 'hello'], 2, 6) == [3, 4, 'hello'] 16 | 17 | function seekAndDestroy() {} 18 | 19 | // CHALLENGE 4: SORT BY HEIGHT 20 | // Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. 21 | // ex. 22 | // a = [-1, 150, 190, 170, -1, -1, 160, 180] 23 | // sortByHeight(a) == [-1, 150, 160, 170, -1, -1, 180, 190] 24 | 25 | function sortByHeight() {} 26 | 27 | // CHALLENGE 5: MISSING LETTERS 28 | // Find the missing letter in the passed letter range and return it. If all letters are present, return undefined 29 | // ex. 30 | // missingLetters("abce") == "d" 31 | // missingLetters("abcdefghjklmno") == "i" 32 | // missingLetters("abcdefghijklmnopqrstuvwxyz") == undefined 33 | 34 | function missingLetters() {} 35 | 36 | // CHALLENGE 6: EVEN & ODD SUMS 37 | // Take in an array and return an array of the sums of even and odd numbers 38 | // ex. 39 | // evenOddSums([50, 60, 60, 45, 71]) == [170, 116] 40 | 41 | function evenOddSums() {} 42 | --------------------------------------------------------------------------------