├── 001-Basics ├── 001-hello.js ├── 002-conditionals.js ├── 003-loops.js ├── 004-functions.js ├── 005-strings.js ├── 006-stringMethods.js └── 007-stringSearchMethods.js ├── 002-Arrays ├── 001-arrays.js ├── 002-arrayMethods.js ├── 003-arraySearchMethods.js ├── 004-arraySortMethods.js ├── 005-rapidCodeRound.js └── 006-IterationMethods │ ├── 001-for-of.js │ ├── 002-for-in.js │ ├── 003-forEach.js │ ├── 004-map.js │ ├── 005-flatMap.js │ ├── 006-filter.js │ ├── 007-reduce.js │ ├── 008-reduceRight.js │ ├── 009-every.js │ ├── 010-some.js │ ├── 011-from.js │ ├── 012-keys.js │ ├── 013-entries.js │ ├── 014-with.js │ └── 015-spread.js ├── 003-Objects ├── 001-objects.js ├── 002-memory.js ├── 003-rapidCodeRound.js ├── 004-generalObjectMethods.js ├── 005-propertyManagementMethods.js └── 006-objectProtectionMethods.js ├── 004-Polyfills ├── PolyfillsForMethods-MachineCoding │ ├── 001-forEach.js │ ├── 002-map.js │ ├── 003-filter.js │ ├── 004-reduce.js │ └── 005-promise.js └── pollyfillsBasics.js ├── 005-ObjectsAndClasses ├── 001-basics.js └── 002-class.js ├── 006-DOM ├── 001-Basics │ ├── External JS │ │ ├── index.html │ │ └── index.js │ └── index.html ├── 002-Example │ ├── index.html │ └── index.js └── 003-EventListeners │ ├── 001-ButtonExample │ ├── index.html │ └── index.js │ └── 002-BasicTodoExample │ ├── README.md │ ├── index.html │ ├── index.js │ └── wireframe.png ├── 007-SpeedJS ├── 001-dataTypes.js ├── 002-arrayAndObjects.js ├── 003-controlFlow.js ├── 004-iteration.js └── 005-functions.js ├── 008-MachineCoding ├── ArrayMethods │ └── iteration.js └── Polyfills │ ├── 001-forEach.js │ ├── 002-map.js │ ├── 003-filter.js │ ├── 004-reduce.js │ └── 005-promise.js ├── 009-Proxy ├── 001-proxy.js └── 002-negativeIndexExample.js ├── 010-ClockProject ├── index.html ├── script.js └── style.css ├── 011-Promise ├── 001-Basics │ ├── index.html │ └── script.js ├── 002-PromiseChaining │ ├── index.html │ └── script.js └── 003-CleanCode │ ├── index.html │ └── script.js ├── 012-Sync-Async-JS ├── 001-syncCode.js ├── 002-asyncCode.js ├── 003-sumSync.js ├── 004-sumAsync.js ├── 005-callbackHell.js ├── 006-fs-promises.js ├── 007-promisification.js ├── 008-async-await.js └── 009-assignment.js ├── 013-LexicalScoping-ClosureFunctions ├── 001-lexicalScope-closureFunction.js ├── 002-counter.js └── 003-memoryLeak-Closure.js ├── 014-Debouncing ├── debounce.js └── debounce.md ├── 015-Throttling ├── throttling.js └── throttling.md ├── 016-CodeChallenges ├── 001-P0 │ ├── p0-challenge-01.js │ ├── p0-challenge-02.js │ └── p0-challenge-03.js ├── 002-Conditionals │ ├── challenge-01.js │ ├── challenge-02.js │ ├── challenge-03.js │ ├── challenge-04.js │ ├── challenge-05.js │ ├── challenge-06.js │ ├── challenge-07.js │ ├── challenge-08.js │ ├── challenge-09.js │ └── challenge-10.js ├── 003-Loops │ ├── challenge-01.js │ ├── challenge-02.js │ ├── challenge-03.js │ ├── challenge-04.js │ ├── challenge-05.js │ ├── challenge-06.js │ ├── challenge-07.js │ ├── challenge-08.js │ ├── challenge-09.js │ └── challenge-10.js ├── 004-Arrays │ ├── challenge-01.js │ ├── challenge-02.js │ ├── challenge-03.js │ ├── challenge-04.js │ ├── challenge-05.js │ ├── challenge-06.js │ ├── challenge-07.js │ ├── challenge-08.js │ ├── challenge-09.js │ └── challenge-10.js ├── 005-Prototypes │ ├── challenge-01.js │ ├── challenge-02.js │ ├── challenge-03.js │ ├── challenge-04.js │ ├── challenge-05.js │ ├── challenge-06.js │ ├── challenge-07.js │ ├── challenge-08.js │ ├── challenge-09.js │ └── challenge-10.js └── 006-Objects │ ├── challenge-01.js │ ├── challenge-02.js │ ├── challenge-03.js │ ├── challenge-04.js │ ├── challenge-05.js │ ├── challenge-06.js │ ├── challenge-07.js │ ├── challenge-08.js │ ├── challenge-09.js │ └── challenge-10.js ├── 017-Practice ├── 001-JS-Basics │ ├── README.md │ ├── exercise-1a.js │ ├── exercise-1b.js │ ├── exercise-1c.js │ ├── exercise-1d.js │ ├── exercise-1e.js │ ├── exercise-1f.js │ ├── exercise-1g.js │ ├── exercise-1h.js │ ├── exercise-1i.js │ └── exercise-1j.js ├── 002-Numbers-Maths │ ├── README.md │ ├── exercise-2a.js │ ├── exercise-2b.js │ ├── exercise-2c.js │ ├── exercise-2d.js │ ├── exercise-2e.js │ ├── exercise-2f.js │ ├── exercise-2g.js │ ├── exercise-2h.js │ ├── exercise-2i.js │ ├── exercise-2j.js │ ├── exercise-2k.js │ ├── exercise-2l.js │ ├── exercise-2m.js │ └── exercise-2n.js ├── 003-Strings │ ├── README.md │ ├── exercise-3a.js │ ├── exercise-3b.js │ ├── exercise-3c.js │ ├── exercise-3d.js │ ├── exercise-3e.js │ ├── exercise-3f.js │ ├── exercise-3g.js │ ├── exercise-3h.js │ ├── exercise-3i.js │ ├── exercise-3j.js │ ├── exercise-3k.js │ ├── exercise-3l.js │ ├── exercise-3m.js │ └── exercise-3n.js ├── 004-console-log │ ├── README.md │ └── exercise-4.html ├── 005-Variables │ ├── README.md │ └── exercise.html ├── 006-Booleans-IfStatements │ ├── README.md │ └── rock-paper-scissors.html ├── 007-Functions │ ├── calculator.html │ └── rock-paper-scissors.html ├── 008-Objects │ ├── calculator.html │ └── rock-paper-scissors.html ├── 009-DOM │ ├── Exercises │ │ ├── exercise-9a.html │ │ ├── exercise-9b.html │ │ ├── exercise-9c.html │ │ ├── exercise-9d.html │ │ ├── exercise-9e.html │ │ ├── exercise-9g.html │ │ └── exercise-9h.html │ ├── Projects │ │ ├── amazon-shipping-calc.html │ │ ├── calculator.html │ │ ├── rock-paper-scisssors.html │ │ └── youtube-subscribe.html │ └── dom.html ├── 010-DOMWithCSS │ ├── 001-SubscribeButton │ │ ├── index.html │ │ ├── script.js │ │ └── style.css │ ├── 002-RockPaperScissors │ │ ├── README.md │ │ ├── images │ │ │ ├── paper-emoji.png │ │ │ ├── rock-emoji.png │ │ │ └── scissors-emoji.png │ │ ├── index.html │ │ ├── script.js │ │ └── style.css │ └── 003-Calculator │ │ ├── index.html │ │ ├── script.js │ │ └── style.css ├── 011-ArraysAndLoops │ ├── Exercises │ │ └── exercise.js │ └── TodoList │ │ ├── index.html │ │ ├── script.js │ │ └── style.css └── 012-AdvancedFunctions │ ├── 12a.js │ ├── 12b.js │ ├── 12c.html │ ├── 12c.js │ ├── 12d.html │ ├── 12d.js │ ├── 12e.html │ ├── 12e.js │ ├── 12f.html │ ├── 12f.js │ ├── 12g.html │ ├── 12g.js │ ├── 12h.html │ ├── 12h.js │ ├── 12i.html │ ├── 12i.js │ ├── 12j.js │ ├── 12k.js │ ├── 12l.js │ ├── 12m.js │ ├── 12n.js │ ├── 12o.js │ └── README.md ├── 018-Projects ├── 001-Clock │ ├── index.html │ ├── script.js │ └── style.css ├── 002-CountdownTimer │ ├── README.md │ ├── index.html │ └── script.js ├── 003-RandomQuoteGenerator │ ├── index.html │ ├── script.js │ └── style.css └── 004-TodoList │ ├── index.html │ ├── script.js │ └── style.css ├── README.md └── Warmup ├── 002Feb08th.js ├── 002Feb09th.js └── 002Feb12th.js /001-Basics/001-hello.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Primitive: 4 | - String 5 | - Numbers 6 | - Integer 7 | - Float 8 | - Boolean 9 | - Null 10 | - Undefined 11 | - Symbol 12 | - BigInt 13 | 14 | Non-Primitive 15 | - Array [] 16 | - Object {} 17 | 18 | Declaration 19 | - let 20 | - const 21 | - var 22 | */ 23 | 24 | function printChai(n){ 25 | console.log("Hello Chai", n); 26 | console.log("Hello,", n, "Chai"); 27 | } 28 | 29 | function printCoffee(n){ 30 | console.log(`Hello ${n} Coffee`); 31 | } 32 | 33 | function addTwo(a, b){ 34 | console.log(a+b); 35 | } 36 | 37 | function mutliplyTwo(a, b){ 38 | return a*b; 39 | } 40 | 41 | printChai(4); 42 | printCoffee(10); 43 | addTwo(10, 20); 44 | mutliplyTwo(10*20); 45 | console.log(mutliplyTwo(10, 40)); 46 | -------------------------------------------------------------------------------- /001-Basics/002-conditionals.js: -------------------------------------------------------------------------------- 1 | let weather = "rainy"; 2 | 3 | /* 4 | == --> Checks if values are equal or not 5 | === --> Checks for value of equality and type of equality 6 | */ 7 | if(weather === "rainy"){ 8 | console.log("It's a rainy day"); 9 | }else if(weather === "cloudy"){ 10 | console.log("It's a cloudy day"); 11 | }else{ 12 | console.log("It's a sunny day"); 13 | } -------------------------------------------------------------------------------- /001-Basics/003-loops.js: -------------------------------------------------------------------------------- 1 | let cars = ["Mercedes", "BMW", "Audi", "Porsche"]; 2 | 3 | for(let i = 0; i < cars.length; i++){ 4 | console.log(cars[i]); 5 | } -------------------------------------------------------------------------------- /001-Basics/005-strings.js: -------------------------------------------------------------------------------- 1 | let car1 = "Mercedes"; 2 | let car2 = 'Bugatti'; 3 | console.log(typeof car1); 4 | console.log(typeof car2); 5 | 6 | // Strings can also be defined as objects 7 | let car3 = new String("Land Rover"); 8 | console.log(typeof car3); 9 | 10 | let str1 = "That is a 'car'"; 11 | let str2 = 'That is a "car"'; 12 | 13 | // Template String/ Template literal 14 | let numOfCars = 10; 15 | let str = `These 16 | are cars. They are 17 | amazingly fast. There are ${numOfCars} cars`; 18 | 19 | let myCar = "It\'s my car"; 20 | 21 | const num = 5; 22 | // String Interpolation 23 | let numCars = `I have ${5} cars`; 24 | 25 | console.log(car1); 26 | console.log(car2); 27 | console.log(car3); 28 | console.log(str1); 29 | console.log(str2); 30 | console.log(str); 31 | console.log(myCar); 32 | console.log(numCars) -------------------------------------------------------------------------------- /001-Basics/007-stringSearchMethods.js: -------------------------------------------------------------------------------- 1 | const bigStr = "Iron Man siad that, I am Iron Man, because his suit is made up of Iron"; 2 | 3 | // indexOf() - index of first occurrence of beginning of the string 4 | const firstIndex = bigStr.indexOf("Iron"); 5 | console.log(firstIndex); 6 | 7 | const firstIndexAfter = bigStr.indexOf("Iron", 10); 8 | console.log(firstIndexAfter); 9 | 10 | //lastIndexOf() - index of last occurrence of beginning of the string 11 | const lastIndex = bigStr.lastIndexOf("Iron"); 12 | console.log(lastIndex); 13 | 14 | // search() - searches a string for a string (or a regular expression) and returns the position of the match. It cannot take second argument (where to begin search) 15 | const searchIndex = bigStr.search("Iron"); 16 | console.log(searchIndex); 17 | 18 | const text = "The rain in SPAIN stays mainly in the plain"; 19 | 20 | // match() - returns an array containing the results of matching a string against a string (or a regular expression) 21 | console.log(text.match("ain")); // Perform a search for "ain" (first occurrence) 22 | console.log(text.match(/ain/)); // Perform a search for "ain" (first occurrence) 23 | console.log(text.match(/ain/g)); // Perform a global search for "ain" (returns all occurrences) 24 | console.log(text.match(/ain/gi)); // Perform a global search for "ain" (returns all occurrences, case-insensitive) 25 | 26 | // matchAll() - returns an iterator containing the results of matching a string against a string (or a regular expression) 27 | console.log(text.matchAll("ain")); 28 | 29 | // includes() 30 | console.log(bigStr.includes("Hulk")); 31 | console.log(bigStr.includes("Iron Man")); 32 | 33 | // startsWith() 34 | console.log(bigStr.startsWith("Iron")); // true 35 | console.log(bigStr.startsWith("Man", 5)); // true 36 | console.log(bigStr.startsWith("Gold")); // false 37 | 38 | // endsWith() 39 | console.log(bigStr.endsWith("on")); // true 40 | 41 | -------------------------------------------------------------------------------- /002-Arrays/001-arrays.js: -------------------------------------------------------------------------------- 1 | // Arrays 2 | 3 | let cars = ["Mercedes", "BMW", "Audi", "Porsche"]; 4 | console.log(cars); 5 | 6 | let fruits = new Array("Apple", "Mango", "Grapes"); 7 | console.log(fruits); 8 | 9 | // Objects 10 | let person = { 11 | firstName: "Tony", 12 | lastName: "Stark", 13 | age: 50 14 | }; 15 | console.log(person); 16 | 17 | 18 | //Arrays are special type of objects 19 | console.log(typeof cars); 20 | console.log(typeof fruits); 21 | console.log(typeof person); 22 | 23 | // Array.isArray() is used to check if given element is an array 24 | console.log(Array.isArray(cars)); 25 | console.log(Array.isArray(fruits)); 26 | console.log(Array.isArray(person)); 27 | 28 | // Converting array to string 29 | const fruitString = fruits.toString(); 30 | console.log(fruitString); 31 | 32 | // Arrays are references 33 | 34 | const arr1 = [1, 2, 3]; 35 | const arr2 = arr1; 36 | 37 | arr2.push(4); 38 | console.log(arr1); // [ 1, 2, 3, 4 ] 39 | console.log(arr2); // [ 1, 2, 3, 4 ] 40 | 41 | const arr3 = [1, 2, 3]; 42 | const arr4 = arr3.slice(); 43 | 44 | arr4.push(4); 45 | console.log(arr3); // [ 1, 2, 3] 46 | console.log(arr4); // [ 1, 2, 3, 4 ] 47 | 48 | 49 | // Destructuring 50 | 51 | const [fisrtValue, secondValue] = [1, 2, 3, 4]; 52 | 53 | console.log(fisrtValue); // 1 54 | console.log(secondValue); // 2 -------------------------------------------------------------------------------- /002-Arrays/002-arrayMethods.js: -------------------------------------------------------------------------------- 1 | let cars = ["Mercedes", "BMW", "Audi", "Porsche"]; 2 | let cars1 = ["Suzuki", "Hyundai", "Honda", "Mahindra", "Tata"]; 3 | let cars2 = ["Rolls Royce", "Bugatti", "Lamborghini"]; 4 | 5 | // length/ size of array 6 | console.log(cars.length); 7 | 8 | // convert array to string, and seperate elements using comma 9 | console.log(cars.toString()); 10 | 11 | // convert array to string, and seperate elements using any seperator 12 | console.log(cars.join("*")); 13 | 14 | // element at specific index 15 | console.log(cars.at(1)); 16 | console.log(cars[1]); 17 | 18 | // adding elements to the end of array 19 | console.log(cars.push("Land Rover")); 20 | console.log(cars); 21 | cars[cars.length] = "Lexus"; 22 | console.log(cars); 23 | 24 | // removing last element from array 25 | console.log(cars.pop()); 26 | console.log(cars); 27 | 28 | // shift() - removes first element from array and moves all other elements to lower index 29 | console.log(cars.shift()); 30 | console.log(cars); 31 | 32 | // unshift() - add elements to the beginning of the array and moves other elements to higher index 33 | console.log(cars.unshift("Jaguar")); 34 | console.log(cars); 35 | 36 | // changing element at particular index 37 | cars[0] = "Opel"; 38 | console.log(cars); 39 | 40 | // Concatenating arrays 41 | const allCars = cars.concat(cars1, cars2); 42 | console.log(allCars); 43 | 44 | // copy array element to another position 45 | cars.copyWithin(1, 4); 46 | console.log(cars); 47 | 48 | // reducing dimension of array 49 | let nums = [[1,2], [3,4], [5,6], [7,8]]; 50 | console.log(nums); 51 | 52 | const newNums = nums.flat(); 53 | console.log(newNums); 54 | 55 | // flatMap() - maps all elements of array, then creates new array by flattening the array 56 | let nums1 = [1,2,3,4,5]; 57 | const mappedNums = nums1.flatMap(x => [x, x*10]); 58 | console.log(mappedNums); 59 | 60 | // splice() - adds new items to array 61 | let phones = ["iPhone", "Samsung", "Motoroala", "Xiaomi", "Asus", "Nothing"]; 62 | let removedPhones = phones.splice(2, 0, "Oppo", "Vivo"); 63 | console.log(removedPhones); 64 | console.log(phones); 65 | // Elements are spliced in at index 2 66 | // 0 elemts are removed 67 | // New array: ['iPhone','Samsung','Oppo','Vivo','Motoroala','Xiaomi','Asus','Nothing'] 68 | 69 | 70 | removedPhones = phones.splice(3, 1, "OnePlus", "Realme", "Honor"); 71 | console.log(removedPhones); 72 | console.log(phones); 73 | // Elements are spliced in at index 3 74 | // 1 elemts are removed (Vivo) 75 | // New array: ['iPhone','Samsung','Oppo','OnePlus','Realme','Honor','Motoroala','Xiaomi','Asus','Nothing'] 76 | 77 | // toSpliced() - splice an array without altering original array (creates a new array) 78 | const newPhones = phones.toSpliced(1, 0, "Lenovo"); 79 | console.log(newPhones); 80 | 81 | 82 | // slice() - slices out a pieceof array into new array 83 | let phones1 = phones.slice(1, 5); 84 | console.log(phones1); 85 | // Element at index 1 is included, element at index 5 is not included -------------------------------------------------------------------------------- /002-Arrays/003-arraySearchMethods.js: -------------------------------------------------------------------------------- 1 | let cars = ["Mercedes", "BMW", "Audi", "Mercedes", "Land Rover", "Mercedes", "Jaguar", "Land Rover"]; 2 | console.log(cars); 3 | 4 | // indexOf() 5 | console.log(cars.indexOf("Mercedes")); // First occurance of element 6 | console.log(cars.indexOf("Mercedes", 1)); // First occurance of element after specified index 7 | console.log(cars.indexOf("Tata")); // Returns -1, if element is not found 8 | 9 | // lastIndexOf() 10 | console.log(cars.lastIndexOf("BMW")); // Last occurance of element 11 | 12 | // includes() 13 | console.log(cars.includes("Land Rover")); // true 14 | console.log(cars.includes("Opel")); // false 15 | 16 | // find() - returns the value of the first array element that passes a test function 17 | // findIndex() - returns the index value of the first array element that passes a test function 18 | // findLast() - starts searching at end, and returns the first array element (from last) that passes a test function 19 | // findLastIndex() - starts searching at end, and returns the index of the first array element (from last) that passes a test function 20 | 21 | let found = cars.find((element) => element === "Land Rover" ); 22 | let foundIndex = cars.findIndex((element) => element === "Land Rover"); 23 | 24 | console.log(found); 25 | console.log(foundIndex); 26 | 27 | const nums = [5, 12, 8, 130, 44]; 28 | found = nums.find((element) => element > 10); 29 | foundIndex = nums.findIndex((element) => element > 10); 30 | console.log(found); 31 | console.log(foundIndex); 32 | 33 | let foundLast = cars.findLast((element) => element === "Land Rover"); 34 | let foundLastIndex = cars.findLastIndex((element) => element === "Land Rover"); 35 | console.log(foundLast); 36 | console.log(foundLastIndex); 37 | 38 | foundLast = nums.findLast((element) => element > 10); 39 | foundLastIndex = nums.findLastIndex((element) => element > 10); 40 | console.log(foundLast); 41 | console.log(foundLastIndex); 42 | 43 | 44 | -------------------------------------------------------------------------------- /002-Arrays/004-arraySortMethods.js: -------------------------------------------------------------------------------- 1 | let cars = ["Mercedes", "BMW", "Audi", "Mercedes", "Land Rover", "Mercedes", "Jaguar", "Land Rover"]; 2 | console.log(cars); 3 | 4 | // reverse() - reverses the array 5 | cars.reverse(); 6 | console.log(cars); 7 | 8 | // sort() - sorts the array alphabetically 9 | cars.sort(); 10 | console.log(cars); 11 | 12 | let cars1 = ["Suzuki", "Hyundai", "Honda", "Mahindra", "Tata"]; 13 | console.log(cars1); 14 | 15 | // toReversed() - reverses the array, without altering the original one 16 | const reversedCars1 = cars1.toReversed(); 17 | console.log(reversedCars1); 18 | 19 | // toSorted() - sorts the array without altering the original one 20 | const sortedCars1 = cars1.toSorted(); 21 | console.log(sortedCars1); 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /002-Arrays/005-rapidCodeRound.js: -------------------------------------------------------------------------------- 1 | // Problem: Create an array containing diff. types of teas 2 | const teas = ["Greean tea", "Black tea", "Oolong tea", "White tea", "Herbal tea"]; 3 | console.log(teas); 4 | 5 | // Problem: Add "Chamomile tea" to the existing list of teas 6 | teas.push("Chamomile tea"); 7 | console.log(teas); 8 | 9 | // Problem: Remove "Oolong tea" from the list of teas 10 | const index = teas.indexOf("Oolong Tea"); 11 | if(index > -1){ 12 | teas.splice(index, 1); 13 | } 14 | 15 | console.log(teas); 16 | 17 | // Problem: Filter the list to only include teas that are caffinated 18 | const caffinatedTeas = teas.filter((tea) => tea != "Herbal tea"); 19 | console.log(caffinatedTeas); 20 | 21 | // Problem: Sort the list of teas in alphabetical order 22 | console.log(teas.sort()); 23 | 24 | // Problem: Use a for loop to print each type of tea in array 25 | for(let i = 0; i < teas.length; i++){ 26 | console.log(teas[i]); 27 | } 28 | 29 | // Problem: Use a for loop to count how many teas are caffinated (excluding "Herbal tea") 30 | let caffinatedCount = 0; 31 | for(let i = 0; i < teas.length; i++){ 32 | if(teas != "Herabl tea"){ 33 | caffinatedCount++; 34 | } 35 | } 36 | console.log(caffinatedCount); 37 | 38 | // Problem: Use a for loop to create a new array with all tea names in uppercase 39 | const upperCaseTeas = []; 40 | for(let i = 0; i < teas.length; i++){ 41 | upperCaseTeas.push(teas[i].toUpperCase()); 42 | } 43 | console.log(upperCaseTeas); 44 | 45 | // Problem: Use a for loop to find tea name with the most characters 46 | let longestTea = ""; 47 | 48 | for(let i = 0; i < teas.length; i++){ 49 | if(teas[i].length > longestTea.length){ 50 | longestTea = teas[i]; 51 | } 52 | } 53 | 54 | console.log(longestTea); 55 | 56 | // Problem: Use a for loop to reverse the order of teas in the array 57 | const reversedArray = [] 58 | for(let i = teas.length-1; i >=0; i--){ 59 | reversedArray.push(teas[i]); 60 | } 61 | 62 | console.log(reversedArray); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/001-for-of.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object (such as Array, String, TypedArray, Map, Set, NodeList (and other DOM collections), as well as the arguments object, generators produced by generator functions, and user-defined iterables.). 4 | 5 | Syntax: 6 | for (variable of iterable) 7 | statement 8 | 9 | */ 10 | 11 | 12 | const cars = ["Mercedes", "BMW", "Audi", "Porsche"]; 13 | 14 | // Accessing the elements 15 | for (const car of cars) { 16 | console.log(car); 17 | } -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/002-for-in.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties. 4 | 5 | 6 | Syntax: 7 | for (const key in object) { 8 | if (Object.prototype.hasOwnProperty.call(object, key)) { 9 | const element = object[key]; 10 | for(const car in carPrices){ 11 | console.log(car); 12 | } 13 | } 14 | } 15 | 16 | */ 17 | 18 | const carPrices = { 19 | "Mercedes" : 10000000, 20 | "BMW" : 20000000, 21 | "Audi" : 30000000, 22 | "Land Rover" : 40000000 23 | } 24 | 25 | for (const car in carPrices) { 26 | console.log(car); // Accessing the key 27 | console.log(carPrices[car]); // Accessing the value aasociated with current key 28 | } 29 | 30 | let cheaperCars = {} 31 | for (const car in carPrices) { 32 | if(carPrices[car] < 40000000){ 33 | cheaperCars[car] = carPrices[car]; 34 | } 35 | } 36 | console.log(cheaperCars); 37 | -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/003-forEach.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | forEach() method executes a provided function (a callback function) once for EACH array element. 4 | 5 | Syntax: 6 | array.forEach(element => { 7 | 8 | }); 9 | 10 | - element is the parameter of the function 11 | 12 | OR 13 | 14 | array.forEach(function(element) { 15 | 16 | }); 17 | 18 | */ 19 | 20 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 21 | 22 | // Accessing the elements 23 | cars.forEach(car => { 24 | console.log(car); 25 | }); 26 | 27 | 28 | // Find luxorious car ("Rolls Royce") 29 | let luxoriousCar = ""; 30 | 31 | cars.forEach(car => { 32 | if(car === "Rolls Royce"){ 33 | luxoriousCar = car; 34 | } 35 | }); 36 | 37 | console.log(luxoriousCar); 38 | 39 | 40 | // Find non-luxorious car (all except "Rolls Royce") 41 | let nonLuxoriousCars = []; 42 | 43 | cars.forEach(car =>{ 44 | if(car === "Rolls Royce"){ 45 | return; 46 | }else{ 47 | nonLuxoriousCars.push(car); 48 | } 49 | }); 50 | 51 | console.log(nonLuxoriousCars); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/004-map.js: -------------------------------------------------------------------------------- 1 | /* 2 | map() method creates a new array, with the results of calling function, on EACH element of array 3 | 4 | */ 5 | 6 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 7 | 8 | // Find non-luxorious car (all except "Rolls Royce") 9 | 10 | const mapCars = cars.map(car =>{ 11 | if(car !== "Rolls Royce"){ 12 | return car; 13 | } 14 | }); 15 | 16 | console.log(mapCars); 17 | 18 | 19 | /* 20 | 21 | Find average rating for each movie 22 | 23 | */ 24 | 25 | let movieRatings = [ 26 | { title: "Movie A", ratings: [4, 5, 3]}, 27 | { title: "Movie B", ratings: [5, 5, 4]}, 28 | { title: "Movie C", ratings: [3, 4, 2]} 29 | ]; 30 | 31 | let averageRatings = movieRatings.map((movie) => { 32 | let sum = movie.ratings.reduce((acc, curr) => acc + curr, 0); 33 | let average = sum/movie.ratings.length; 34 | 35 | return {title: movie.title, averageRatings: average.toFixed(2)}; 36 | }); 37 | 38 | console.log(averageRatings); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/005-flatMap.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | flatmap() method first maps all elements of an array and then creates a new array by flattening the array 4 | 5 | combination of map() and flat() 6 | 7 | */ 8 | 9 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 10 | 11 | const newCars = cars.flatMap(car => { 12 | return car.padStart(car.length+3, "new"); 13 | }); 14 | 15 | console.log(newCars); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/006-filter.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | filter() method creates a new array with array elements that pass a test. 4 | 5 | */ 6 | 7 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 8 | 9 | const longCars = cars.filter(car => car.length >= 5); 10 | console.log(longCars); 11 | 12 | 13 | /* 14 | 15 | Find the pending tasks according to thier priority 16 | 17 | */ 18 | 19 | let tasks = [ 20 | { description: "Write Email", completed: false, priority: 2}, 21 | { description: "Send Email", completed: true, priority: 3}, 22 | { description: "Prepare presentation", completed: false, priority: 1} 23 | ]; 24 | 25 | const pendingSortedTasks = tasks 26 | .filter((task) => !(task.completed)) 27 | .sort((a, b) => a.priority - b.priority); 28 | 29 | console.log(pendingSortedTasks); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/008-reduceRight.js: -------------------------------------------------------------------------------- 1 | /* 2 | reduceRight() method works similar to reduce(), but from RIGHT-to-LEFT 3 | */ 4 | 5 | // Exa: Reversing an Array 6 | 7 | let cars = ['Mercedes', 'BMW', 'Audi']; 8 | 9 | let reversedCars = cars.reduceRight((accumulator, currentValue) => { 10 | accumulator.push(currentValue); 11 | return accumulator; 12 | }, []); 13 | 14 | console.log(reversedCars); // ['Audi', 'BMW', 'Mercedes'] 15 | 16 | 17 | // Exa: Flattening array but in reverse order 18 | let nestedArray = [[1, 2], [3, 4], [5, 6]]; 19 | 20 | let flattened = nestedArray.reduceRight((accumulator, currentValue) => { 21 | return accumulator.concat(currentValue); 22 | }, []); 23 | 24 | console.log(flattened); // [5, 6, 3, 4, 1, 2] 25 | -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/009-every.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | every() method checks if all the array values pass a test 4 | 5 | */ 6 | 7 | const numbers = [45, 4, 9, 16, 25]; 8 | 9 | const ifAllGreaterThan10 = numbers.every( num => num > 10); 10 | console.log(ifAllGreaterThan10); 11 | 12 | const ifAllLesserThan100 = numbers.every( num => num < 100); 13 | console.log(ifAllLesserThan100); 14 | 15 | -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/010-some.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | some() method checks if if some array values pass a test 4 | 5 | */ 6 | 7 | const numbers = [45, 4, 9, 16, 25]; 8 | 9 | const ifSomeGreaterThan10 = numbers.some( num => num > 10); 10 | console.log(ifSomeGreaterThan10); 11 | 12 | const ifSomeLesserThan0 = numbers.some( num => num < 0); 13 | console.log(ifSomeLesserThan0); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/011-from.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Array.from() method returns an Array object from any object with a length property or any iterable object 4 | 5 | */ 6 | 7 | const newArray = Array.from("ABCDE"); 8 | console.log(newArray); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/012-keys.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | keys() method returns an Array Iterator object with the keys of an array 4 | 5 | */ 6 | 7 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 8 | 9 | const carKeys = cars.keys(); 10 | 11 | for (const key of carKeys) { 12 | console.log(key); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/013-entries.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | entries() method returns an Array Iterator object with key/value pairs 4 | 5 | */ 6 | 7 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 8 | 9 | const carEntries = cars.entries(); 10 | 11 | for (const car of carEntries) { 12 | console.log(car); 13 | } -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/014-with.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | with() method is a safe way to update elements in an array without altering the original array 4 | 5 | */ 6 | 7 | const cars = ["Mercedes", "BMW", "Audi", "Porsche", "Rolls Royce", "Bugatti", "Lamborghini"]; 8 | 9 | const updatedCars = cars.with(0, "Mercedes Benz"); 10 | 11 | console.log(updatedCars); -------------------------------------------------------------------------------- /002-Arrays/006-IterationMethods/015-spread.js: -------------------------------------------------------------------------------- 1 | /* 2 | - ... (spread) allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array) are expected. 3 | 4 | - In an object, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created. 5 | 6 | */ 7 | 8 | sum = (x, y, z, w, v) => { 9 | return x+y+z+w+v; 10 | } 11 | const numbers = [1, 2, 3, 4, 5]; 12 | 13 | console.log(sum(...numbers)); -------------------------------------------------------------------------------- /003-Objects/003-rapidCodeRound.js: -------------------------------------------------------------------------------- 1 | // Problem: Create an object representing a type of tea with properties for name, type and caffeine content 2 | const tea = { 3 | teaName : "Lemon Tea", 4 | teaType : "Green Tea", 5 | caffineContent : "Low" 6 | } 7 | 8 | // Problem: Accesss and print the name and type properties of the tea object 9 | console.log(tea.teaName); 10 | console.log(tea["teaType"]); 11 | 12 | // Problem: Add a new property origin to the tea object 13 | tea.origin = "Assam"; 14 | console.log(tea); 15 | 16 | // Problem: Change the caffeine level of the tea object to 'medium' 17 | tea.caffineContent = "Medium"; 18 | console.log(tea); 19 | 20 | // Problem: Remove the type property from the tea object 21 | delete tea.teaType; 22 | console.log(tea); 23 | 24 | // Problem: Check if tea object has a property origin 25 | console.log("origin" in tea); 26 | 27 | // Problem: Use for...in loop to print all properties of the tea object 28 | for (const key in tea) { 29 | console.log(`${key} : ${tea[key]}`); 30 | } 31 | 32 | // Problem: Create a nested object representing different types of teas and their properties 33 | const myTeas = { 34 | greenTea : { 35 | teaName : "Green Tea", 36 | cups : { 37 | one : "1", 38 | two : "2" 39 | } 40 | }, 41 | blackTea : { 42 | teaName : "Black Tea" 43 | } 44 | } 45 | 46 | // Problem : Create a copy of tea object 47 | 48 | // Shallow Copy 49 | const teaCopy = { 50 | ...myTeas 51 | } // shallow copy (only first level keys are copied) 52 | console.log(teaCopy); 53 | 54 | // Reference 55 | const anotherCopy = myTeas; // Passing reference (anotherCopy is a reference) 56 | 57 | // Deep Copy 58 | const myTeasString = JSON.stringify(myTeas); 59 | const myTeasCopy = JSON.parse(myTeasString); 60 | 61 | console.log(myTeasCopy); 62 | 63 | // Problem : Add a custom method describe to the tea object that returns a description string 64 | // Problem : Merge two objects representing different teas into one 65 | -------------------------------------------------------------------------------- /003-Objects/004-generalObjectMethods.js: -------------------------------------------------------------------------------- 1 | /* 2 | - Object.hasOwn(object, property) 3 | - Object.assign(target, source) 4 | - Object.create(object) 5 | - Object.entries(object) 6 | - Object.fromEntries() 7 | - Object.keys(object) 8 | - Object.values(object) 9 | - Object.groupBy(object, callback) 10 | */ 11 | 12 | const car1 = { 13 | model : "Land Rover", 14 | brand : "Range Rover", 15 | color : "Black" 16 | } 17 | 18 | const car2 = { 19 | model : "Defender", 20 | color : "Red", 21 | parentBrand : "TATA" 22 | } 23 | 24 | console.log(car1); 25 | console.log(car2); 26 | 27 | // Object.hasOwn(object, property) - returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false. 28 | 29 | console.log(Object.hasOwn(car1, "brand")); 30 | 31 | // Object.assign(target, source) - Copies enumerable own properties from a source object to a target object, returns modified target object 32 | Object.assign(car1, car2); 33 | 34 | console.log(car1); 35 | console.log(car2); 36 | 37 | 38 | // Object.create(object) - Creates an object from an existing object as the prototype of the newly created object 39 | const car3 = Object.create(car2); 40 | 41 | console.log(car3); // {} 42 | 43 | car3.model = "Discovery"; 44 | car3.brand = "Land Rover"; 45 | car3.price = "INR 70,00,000/-" 46 | 47 | console.log(car3); 48 | 49 | 50 | // Object.entries(object) - Returns an ARRAY of the given object's own ennumerable [key, value] pairs 51 | const objectEntries = Object.entries(car3); 52 | console.log(objectEntries); 53 | 54 | // can be used with loops 55 | for(let [key, value] of Object.entries(car2)){ 56 | console.log([key, value]); 57 | } 58 | 59 | // makes it simple to convert OBJECTS to MAP 60 | const myMap = new Map(Object.entries(car2)); 61 | console.log(myMap); 62 | 63 | 64 | // Object.fromEntries() - Creates an object from a list of keys/values 65 | const fruits = [ 66 | ["apples", 300], 67 | ["pears", 900], 68 | ["bananas", 500] 69 | ]; 70 | 71 | const fruitObject = Object.fromEntries(fruits); 72 | console.log(fruitObject); 73 | 74 | 75 | // Object.keys(object) - Returns an array of the enumerable keys of an object 76 | const keyArray = Object.keys(fruitObject); 77 | console.log(keyArray); 78 | 79 | // Object.values(object) - Returns an array of the enumerable property values of an object 80 | const valueArray = Object.values(fruitObject); 81 | console.log(valueArray); 82 | 83 | 84 | // Object.groupBy(object, callback) - groups elements of an object according to string values returned from a callback function. Works only with enumerable properties. 85 | 86 | const myFruits = [ 87 | {name:"apples", quantity:300}, 88 | {name:"bananas", quantity:500}, 89 | {name:"oranges", quantity:200}, 90 | {name:"kiwi", quantity:150} 91 | ]; 92 | 93 | function myCallback({ quantity }) { 94 | return quantity > 200 ? "ok" : "low"; 95 | } 96 | 97 | const result = Object.groupBy(myFruits, myCallback); 98 | console.log(result); 99 | 100 | -------------------------------------------------------------------------------- /003-Objects/005-propertyManagementMethods.js: -------------------------------------------------------------------------------- 1 | /* 2 | - Object.defineProperty(object, property, descriptor) 3 | - Object.defineProperties(object, descriptors) 4 | - Object.getOwnPropertyDescriptor(object, property) 5 | - Object.getOwnPropertyDescriptors(object) 6 | - Object.getOwnPropertyNames(object) 7 | - Object.getPrototypeOf(object) 8 | */ 9 | 10 | // Object.defineProperty(object, property, descriptor) 11 | // Adding a new property to an object 12 | // Changing property values 13 | // Changing property metadata 14 | // Changing object getters and setters 15 | 16 | const person1 = { 17 | firstName: "Tony", 18 | lastName : "Stark" 19 | }; 20 | 21 | const person2 = { 22 | firstName : "Steve", 23 | lastName : "Rogers", 24 | age : "45" 25 | } 26 | 27 | Object.defineProperty(person1, "age", {value:"50"}); 28 | console.log(person1); // age property exits but will not be shown in output, because properties added using Object.defineProperty() are non-enumerable (enumerable : false). They are also non-writable(writable : false), non-configurable (configurable : false). 29 | console.log(person1.age); 30 | 31 | Object.defineProperty(person1, "superhero", {value : "Iron Man", enumerable : true}); 32 | console.log(person1); 33 | 34 | 35 | // Object.defineProperties(object, descriptors) - Adding or changing multiple object properties 36 | Object.defineProperties(person1, { 37 | powerSource : { 38 | value : "Arc Reactor", 39 | enumerable : true 40 | }, 41 | suits : { 42 | value : ["Mark 1", "Mark 10", "Mark 42"], 43 | enumerable : true 44 | }, 45 | industries : { 46 | value : { 47 | name : "Stark Industires", 48 | doamin : "Engineering", 49 | CEO : "Pepper Pots", 50 | owner : "Tony Strak" 51 | }, 52 | enumerable: true 53 | } 54 | }); 55 | 56 | console.log(person1); 57 | 58 | 59 | // Object.getOwnPropertyDescriptor(object, property) - returns an object describing the configuration of a specific property on a given object 60 | const descriptor1 = Object.getOwnPropertyDescriptor(person1, "superhero"); 61 | console.log(descriptor1); 62 | 63 | // Object.getOwnPropertyDescriptors(object) - returns an object describing the configuration of all properties on a given object 64 | const descriptor2 = Object.getOwnPropertyDescriptors(person1); 65 | console.log(descriptor2); 66 | 67 | 68 | // Object.getOwnPropertyNames(object) - returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object. 69 | 70 | const propertyNames = Object.getOwnPropertyNames(person1); 71 | console.log(propertyNames); 72 | 73 | 74 | // Object.getPrototypeOf(object) - returns the prototype 75 | const prototype1 = Object.getPrototypeOf(person1); 76 | console.log(prototype1); 77 | 78 | console.log(Object.getPrototypeOf(person1) === Object.getPrototypeOf(person2)); 79 | 80 | -------------------------------------------------------------------------------- /003-Objects/006-objectProtectionMethods.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | - const 4 | - Object.preventExtensions(object) 5 | - Object.isExtensible(object) 6 | - Object.seal(object) 7 | - Object.isSealed(object) 8 | - Object.freeze(object) 9 | - Object.isFrozen(object) 10 | 11 | */ 12 | 13 | // const - prevents re-assignment (can still change the value of a property, delete a property or create a new property) 14 | const person = { 15 | firstName: 'Tony', 16 | lastName: 'Stark', 17 | superhero: 'Iron Man', 18 | powerSource: 'Arc Reactor', 19 | suits: [ 'Mark 1', 'Mark 10', 'Mark 42' ], 20 | industries: { 21 | name: 'Stark Industires', 22 | doamin: 'Engineering', 23 | CEO: 'Pepper Pots', 24 | owner: 'Tony Strak' 25 | } 26 | } 27 | 28 | 29 | // Object.preventExtensions(object) - prevents new properties from ever being added to an object. It also prevents the object's prototype from being re-assigned. 30 | Object.preventExtensions(person); 31 | 32 | // Object.defineProperty(person, "car", {value : "R8"}); -> TypeError 33 | 34 | 35 | // Object.isExtensible(object) - Returns true if properties can be added to an object 36 | console.log(Object.isExtensible(person)); 37 | 38 | 39 | // Object.seal(object) - Sealing an object prevents extensions and makes existing properties non-configurable. A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned. Values of existing properties can still be changed as long as they are writable. seal() returns the same object that was passed in. 40 | 41 | 42 | // Object.isSealed(object) - returns true if object is sealed 43 | 44 | 45 | // Object.freeze(object) - prevents extensions and makes existing properties non-writable and non-configurable. A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed, their enumerability, configurability, writability, or value cannot be changed, and the object's prototype cannot be re-assigned. freeze() returns the same object that was passed in. 46 | 47 | 48 | // Object.isFrozen() - returns true if an object is frozen. 49 | 50 | 51 | -------------------------------------------------------------------------------- /004-Polyfills/PolyfillsForMethods-MachineCoding/001-forEach.js: -------------------------------------------------------------------------------- 1 | // Exa: Error: .forEach function does not exist on arr variable 2 | 3 | // How to approach - Understand real signature of funtion 4 | 5 | const arr = [10,20,30]; 6 | 7 | const res = arr.forEach(function(value, index){ 8 | console.log(`Value at ${index} is ${value}`); 9 | }); 10 | 11 | const userFunction = function(value, index){ 12 | console.log(`Value: ${value}, Index: ${index}`); 13 | } 14 | const result = arr.forEach(userFunction); 15 | 16 | // console.log(`Returned: ${res}`); 17 | 18 | // Signature: 19 | // parameter for forEach -> function; function parameters -> index, value 20 | // what forEach does? -> calls function for every value 21 | // return -> no return 22 | 23 | // Writing Polyfill for forEach() 24 | 25 | if(!Array.prototype.myForEach){ 26 | 27 | Array.prototype.myForEach = function(userFunction){ 28 | 29 | const originalArr = this; // Points to current object 30 | 31 | for(let i = 0; i < originalArr.length; i++){ 32 | userFunction(originalArr[i], i); 33 | } 34 | } 35 | } 36 | 37 | const myResult = arr.myForEach(userFunction); -------------------------------------------------------------------------------- /004-Polyfills/PolyfillsForMethods-MachineCoding/002-map.js: -------------------------------------------------------------------------------- 1 | // Signature: 2 | // parameter -> callback 3 | // return -> new array (with result of work asked to be done by user) 4 | // does not modify original array 5 | // performs specified operation on each element, and push the result of that operation into new array 6 | 7 | const multiplyBy10 = function(value){ 8 | return value*10; 9 | } 10 | 11 | const arr = [1, 2, 3]; 12 | 13 | const result = arr.map(multiplyBy10); 14 | 15 | console.log(result); 16 | 17 | // Writing pollyfill for map() 18 | 19 | if(!Array.prototype.myMap){ 20 | 21 | Array.prototype.myMap = function(userFunction){ 22 | 23 | const mapResult = []; 24 | 25 | for(let i = 0; i < this.length; i++){ 26 | mapResult.push(userFunction(this[i])); 27 | } 28 | 29 | return mapResult; 30 | } 31 | } 32 | 33 | const myResult = arr.myMap(multiplyBy10); 34 | console.log(myResult); -------------------------------------------------------------------------------- /004-Polyfills/PolyfillsForMethods-MachineCoding/003-filter.js: -------------------------------------------------------------------------------- 1 | // Signature: 2 | // Parameter -> callback 3 | // Return -> returns a new array, with only those elements that pass the test 4 | // Functionality -> calls function for every value, and adds current value to the result array if it passes the test (returns true) 5 | 6 | 7 | 8 | 9 | const divisibleBy3 = function(value){ 10 | if(value%3 === 0){ 11 | return true; 12 | }else{ 13 | return false; 14 | } 15 | } 16 | 17 | const arr = [10, 20, 30, 40, 50, 60]; 18 | 19 | const result = arr.filter(divisibleBy3); 20 | console.log(result); 21 | 22 | if(!Array.prototype.myFilter){ 23 | 24 | Array.prototype.myFilter = function(userFunction){ 25 | 26 | const filterResult = []; 27 | 28 | for(let i = 0; i < this.length; i++){ 29 | if(userFunction(this[i])){ 30 | filterResult.push(this[i]); 31 | } 32 | } 33 | 34 | return filterResult; 35 | } 36 | } 37 | 38 | const myResult = arr.myFilter(divisibleBy3); 39 | console.log(myResult); -------------------------------------------------------------------------------- /004-Polyfills/PolyfillsForMethods-MachineCoding/004-reduce.js: -------------------------------------------------------------------------------- 1 | const arr = [1, 2, 3, 4, 5]; 2 | 3 | console.log(arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0)); 4 | console.log(arr.reduce((accumulator, currentValue) => accumulator + currentValue, 15)); 5 | console.log(arr.reduce((accumulator, currentValue) => accumulator + currentValue)); 6 | 7 | 8 | if (!Array.prototype.myReduce){ 9 | 10 | Array.prototype.myReduce = function(cb, initialValue = undefined){ 11 | 12 | let acc = initialValue || this[0]; 13 | 14 | let startIndex = initialValue ? 0 : 1; 15 | 16 | for(let i = startIndex; i < this.length; i++){ 17 | acc = cb(acc, this[i]); 18 | } 19 | 20 | return acc; 21 | } 22 | } 23 | 24 | console.log(arr.myReduce((acc, curr) => acc + curr, 0)); 25 | console.log(arr.myReduce((acc, curr) => acc + curr, 15)); 26 | console.log(arr.myReduce((acc, curr) => acc + curr)); -------------------------------------------------------------------------------- /004-Polyfills/pollyfillsBasics.js: -------------------------------------------------------------------------------- 1 | // Prototype Basics 2 | 3 | console.log(Array.prototype); 4 | 5 | const arr = [1,2,3] 6 | 7 | console.log(arr.__proto__); 8 | 9 | Array.prototype.ironMan = function(){ 10 | return "I am Iron Man"; 11 | } 12 | 13 | const res = arr.ironMan(); 14 | console.log(res); 15 | 16 | 17 | // Polyfill basics 18 | 19 | if(!Array.prototype.newFunction){ 20 | 21 | // We have an option to throw error ("Please update your browser") 22 | 23 | // OR 24 | 25 | // We can explicitly define it 26 | Array.prototype.fill = function(){ 27 | 28 | }; 29 | 30 | // Fallback - Polyfill 31 | } 32 | 33 | arr.fill(); 34 | 35 | -------------------------------------------------------------------------------- /005-ObjectsAndClasses/001-basics.js: -------------------------------------------------------------------------------- 1 | const obj1 = { 2 | fname : 'Tony', 3 | lname : 'Stark', 4 | getFullName : function(){ 5 | return `${this.fname} ${this.lname}`; 6 | } 7 | }; 8 | 9 | const obj2 = { 10 | fname : 'Steve', 11 | lname : 'Rogers', 12 | getFullName : function(){ 13 | return `${this.fname} ${this.lname}`; 14 | } 15 | }; 16 | 17 | // DRY - Do Not Repeat Yourself, is violated here 18 | console.log(obj1.getFullName()); 19 | console.log(obj1.getFullName()); 20 | 21 | const obj3 = { 22 | fname : 'Bruce', 23 | lname : 'Banner', 24 | }; 25 | 26 | // console.log(obj3.getFullName()); 27 | // This will throw error. 28 | // The code will first check in obj3 if getFullName exist 29 | // If the method is not found, it will check in __proto__ (points to predefined prototypes of Objects) 30 | // Other functions will work 31 | 32 | console.log(obj3.toString()); 33 | 34 | obj3.__proto__ = obj2; // now __proto__ in obj3 points to obj1 35 | 36 | console.log(obj3.getFullName()); // now this works 37 | 38 | console.log(obj3.toString()); // This will also work 39 | // __proto__ of obj3 is pointing to obj1, which has __proto__ that points to prototypes of objects 40 | // It will continue to look until it finds null 41 | 42 | console.log(obj1.__proto__.__proto__); // returns null 43 | 44 | obj3.__proto__ = null; 45 | // console.log(obj3.toString()); 46 | // This will throw error because now we broke the chain 47 | 48 | // Prototype Inheritance/ Prototype Chaining 49 | 50 | 51 | // Why everything in JS is object 52 | 53 | const arr = [1, 2, 3]; 54 | console.log(arr.__proto__); // Object (Array) 55 | console.log(arr.__proto__.__proto__); // Object 56 | 57 | const string = "ThisIsAString"; 58 | console.log(string.__proto__); // String 59 | console.log(string.__proto__.__proto__); // Object 60 | -------------------------------------------------------------------------------- /005-ObjectsAndClasses/002-class.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | 3 | // Parameterized Constructor 4 | constructor(fname, lname){ 5 | this.fname = fname; 6 | this.lname = lname; 7 | } 8 | 9 | // If we don't write a constructor, Deafult Constructor is initialized 10 | // constructor(){ 11 | // } 12 | 13 | getFullName(){ 14 | return `${this.fname} ${this.lname}`; 15 | } 16 | } 17 | 18 | const p1 = new Person("Tony", "Stark"); 19 | const p2 = new Person("Bruce", "Banner"); 20 | 21 | console.log(p1.getFullName()); 22 | console.log(p2.getFullName()); 23 | 24 | console.log(Person.prototype); // Object 25 | console.log(p1.__proto__); // Object 26 | 27 | // Here we let JavaScript handled the prototype by itself 28 | // Whenever we create a new object (say obj1), it gets the prototype from Person class 29 | // __p1.proto__ = Person.prototype 30 | 31 | // Since, __proto__ of obj1 and obj2 are same. We say that obj1 and obj2 belong to same class 32 | 33 | 34 | // Code looks clean now - Syntax Sugar -------------------------------------------------------------------------------- /006-DOM/001-Basics/External JS/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /006-DOM/001-Basics/External JS/index.js: -------------------------------------------------------------------------------- 1 | console.log(window); 2 | // NodeJS -> Reference Error: window is not defined 3 | // We can use window only in browser 4 | 5 | window.document.write("Hello From JS"); 6 | // This will write Hello from JS on web page (we have manipulated the HTML of website) -------------------------------------------------------------------------------- /006-DOM/001-Basics/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 11 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /006-DOM/002-Example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /006-DOM/002-Example/index.js: -------------------------------------------------------------------------------- 1 | function changeBackgroundColorBlack(){ 2 | document.body.style.backgroundColor = 'black'; 3 | } 4 | 5 | function changeBackgroundColorWhite(){ 6 | document.body.style.background = 'white'; 7 | } 8 | 9 | // DRY is violated 10 | 11 | 12 | // Fixing DRY 13 | 14 | function changeBackgroundColor(color){ 15 | document.body.style.backgroundColor = color; 16 | } -------------------------------------------------------------------------------- /006-DOM/003-EventListeners/001-ButtonExample/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /006-DOM/003-EventListeners/001-ButtonExample/index.js: -------------------------------------------------------------------------------- 1 | function changeBackgroundColor(color){ 2 | document.body.style.backgroundColor = color; 3 | } 4 | 5 | // referencing dark-button 6 | const darkButton = document.getElementById("dark-button"); 7 | 8 | // Check in browser console, of we targeted the correct button 9 | console.log(darkButton); 10 | 11 | // Adding event listener to button 12 | // params -> event, listener function 13 | // whenever the event occurs, listener function is executed 14 | darkButton.addEventListener('click', function(){ 15 | console.log('I got clicked'); 16 | changeBackgroundColor('black'); 17 | }); 18 | 19 | // can we perform another function on same button? -> YES 20 | 21 | darkButton.addEventListener('click', function(){ 22 | console.log('Another function'); 23 | }); 24 | 25 | 26 | // referencing theme-button 27 | const themeButton = document.getElementById("theme-button"); 28 | console.log(themeButton); 29 | 30 | themeButton.addEventListener('click', function(){ 31 | console.log(document.body.style.backgroundColor); 32 | 33 | const currentColor = document.body.style.backgroundColor; 34 | 35 | if(!currentColor || currentColor == "white"){ 36 | changeBackgroundColor("black"); 37 | themeButton.innerText = "Light Mode"; // Overwriting the button label 38 | }else{ 39 | changeBackgroundColor("white"); 40 | themeButton.innerText = "Dark Mode"; // Overwriting the button label 41 | } 42 | }); -------------------------------------------------------------------------------- /006-DOM/003-EventListeners/002-BasicTodoExample/README.md: -------------------------------------------------------------------------------- 1 | ### Todo App 2 | 3 | ## Wireframe 4 | ![alt text](wireframe.png) 5 | 6 | ## Functionality 7 | 8 | - **Add Task:** Users can add a task by typing in the textbox and clicking the 'Add' button. 9 | - **Delete Task:** Users can delete individual tasks by clicking the 'Delete' button next to the task. 10 | - **Clear All Tasks:** Users can clear the entire list of tasks by clicking the 'Delete All' button. -------------------------------------------------------------------------------- /006-DOM/003-EventListeners/002-BasicTodoExample/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Todo App 7 | 8 | 9 |

Todo App

10 |
11 | 12 | 13 |
14 | 15 | 16 | 17 |
18 | 19 | 20 |
21 | 23 |
24 |
25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /006-DOM/003-EventListeners/002-BasicTodoExample/index.js: -------------------------------------------------------------------------------- 1 | console.log('Todo App'); 2 | 3 | // Referencing todo-input 4 | const todoInput = document.getElementById('todo-input'); 5 | 6 | // Referencing add-button 7 | const addButton = document.getElementById('add-button'); 8 | 9 | // Referencing to-do-items-container 10 | const todoItemsContainer = document.getElementById('to-do-items-container'); 11 | 12 | // Referencing delete-all-button 13 | const deleteAllButton = document.getElementById('delete-all-button') 14 | 15 | // Adding event listener to addButton 16 | addButton.addEventListener('click', function(){ 17 | // console.log(addButton); 18 | 19 | // Getting user input 20 | const inputValue = todoInput.value; 21 | console.log(inputValue); 22 | 23 | // Make
  • tag for each input 24 | const li = document.createElement('li'); //
  • 25 | console.log(li); 26 | 27 | // Adding value to
  • tag 28 | li.innerText = inputValue; //
  • (value)
  • 29 | console.log(li); 30 | 31 | // Creating delete button 32 | const deleteButton = document.createElement('button'); 33 | deleteButton.innerText = 'Clear'; 34 | 35 | // Adding event listener to deleteButton 36 | deleteButton.addEventListener('click', function(){ 37 | li.remove(); 38 | }); 39 | 40 | // Adding delete button (adding delete button to each li) 41 | li.appendChild(deleteButton); 42 | 43 | 44 | // Displaying tasks on screen (adding
  • to