├── 01 - Create Hello World Function.js ├── 02 - Counter.js ├── 03 - To Be Or Not To Be.js ├── 04 - Counter II.js ├── 05 -Apply Transform Over Each Element in Array.js ├── 06 - Filter Elements from Array.js ├── 07 - Array Reduce Transformation.js ├── 08 - Function Composition.js ├── 09 - Return Length of Arguments Passed.js ├── 10 - Allow One Function Call.js ├── 11 - Memoize.js ├── 12 - Add Two Promises.js ├── 13 - Sleep.js ├── 14 - Timeout Cancellation.js ├── 15 - Interval Cancellation.js ├── 16 - Promise Time Limit.js ├── 17 - Cache With Time Limit.js ├── 18 - Debounce.js ├── 19 - Execute Asynchronous Functions in Parallel.js ├── 20 - Is Object Empty.js ├── 21 - Chunk Array.js ├── 22 - Array Prototype Last.js ├── 23 - Group By.js ├── 24 - Sort By.js ├── 25 - Join Two Arrays by ID.js ├── 26 - Flatten Deeply Nested Array.js └── 27 - Compact Object.js /01 - Create Hello World Function.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/create-hello-world-function/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var createHelloWorld = function() { 9 | return function(...args) { 10 | return "Hello World"; 11 | } 12 | }; 13 | 14 | /** 15 | * const f = createHelloWorld(); 16 | * f(); // "Hello World" 17 | */ -------------------------------------------------------------------------------- /02 - Counter.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/counter/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var createCounter = function(n) { 9 | return function() { 10 | return n++; 11 | }; 12 | }; 13 | 14 | /** 15 | * const counter = createCounter(10) 16 | * counter() // 10 17 | * counter() // 11 18 | * counter() // 12 19 | */ -------------------------------------------------------------------------------- /03 - To Be Or Not To Be.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/to-be-or-not-to-be/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | var expect = function(val) { 8 | return { 9 | toBe: (val2) => { 10 | if (val !== val2) throw new Error("Not Equal"); 11 | else return true; 12 | }, 13 | notToBe: (val2) => { 14 | if (val === val2) throw new Error("Equal"); 15 | else return true; 16 | } 17 | } 18 | }; -------------------------------------------------------------------------------- /04 - Counter II.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/counter-ii/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | var createCounter = function(init) { 8 | let presentCount = init; 9 | 10 | function increment() { 11 | return ++presentCount; 12 | } 13 | 14 | function decrement() { 15 | return --presentCount; 16 | } 17 | 18 | function reset() { 19 | return (presentCount = init); 20 | } 21 | 22 | return { increment, decrement, reset }; 23 | }; -------------------------------------------------------------------------------- /05 -Apply Transform Over Each Element in Array.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/apply-transform-over-each-element-in-array/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var map = function(arr, fn) { 9 | const ans=[]; 10 | for(let i=0;i size) { 18 | arr.pop(); 19 | } 20 | return arr 21 | }; -------------------------------------------------------------------------------- /07 - Array Reduce Transformation.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/array-reduce-transformation/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | var reduce = function(nums, fn, init) { 8 | let val = init; 9 | for (let i = 0; i < nums.length; i++) { 10 | val = fn(val, nums[i]); 11 | } 12 | return val; 13 | }; -------------------------------------------------------------------------------- /08 - Function Composition.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/function-composition/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var compose = function(functions) { 9 | if (functions.length === 0) { 10 | return function(x) { return x; }; 11 | } 12 | 13 | return functions.reduceRight(function(prevFn, nextFn) { 14 | return function(x) { 15 | return nextFn(prevFn(x)); 16 | }; 17 | }); 18 | 19 | }; 20 | 21 | 22 | const fn = compose([x => x + 1, x => 2 * x]); 23 | console.log(fn(4)); // 9 -------------------------------------------------------------------------------- /09 - Return Length of Arguments Passed.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/return-length-of-arguments-passed/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | function argumentsLength() { 9 | return arguments.length 10 | }; -------------------------------------------------------------------------------- /10 - Allow One Function Call.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/allow-one-function-call/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | var once = function(fn) { 8 | let c=0; //counter variable 9 | return function(...args){ 10 | if(c==0){ 11 | c++; 12 | return fn(...args); 13 | } 14 | else{ 15 | return undefined; 16 | } 17 | } 18 | }; -------------------------------------------------------------------------------- /11 - Memoize.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/memoize/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | function memoize(fn) { 8 | 9 | const cache = {}; 10 | 11 | return function(...args) { 12 | const key = JSON.stringify(args); 13 | 14 | if (key in cache) { 15 | return cache[key]; 16 | } 17 | 18 | const result = fn.apply(this, args); 19 | cache[key] = result; 20 | 21 | return result; 22 | } 23 | 24 | } 25 | 26 | 27 | const memoizedSum = memoize(function(a, b) { 28 | return a + b; 29 | }); 30 | 31 | console.log(memoizedSum(2, 3)); // Output: Computing sum, 5 32 | console.log(memoizedSum(2, 3)); // Output: 5 -------------------------------------------------------------------------------- /12 - Add Two Promises.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/add-two-promises/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var addTwoPromises = async function(promise1, promise2) { 9 | // Wait for both promises to resolve and retrieve their values 10 | const [value1, value2] = await Promise.all([promise1, promise2]); 11 | 12 | // Return a new promise that resolves with the sum of the values 13 | return value1 + value2; 14 | }; 15 | 16 | // // Example usage: 17 | // var promise1 = new Promise(resolve => setTimeout(() => resolve(2), 20)); 18 | // var promise2 = new Promise(resolve => setTimeout(() => resolve(5), 60)); 19 | 20 | // addTwoPromises(promise1, promise2) 21 | // .then(console.log); // Output: 7 -------------------------------------------------------------------------------- /13 - Sleep.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/sleep/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | async function sleep(millis) { 9 | await new Promise(resolve => setTimeout(resolve, millis)); 10 | } 11 | 12 | /** 13 | * let t = Date.now() 14 | * sleep(100).then(() => console.log(Date.now() - t)) // 100 15 | */ -------------------------------------------------------------------------------- /14 - Timeout Cancellation.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/timeout-cancellation/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | const cancellable = function(fn, args, t) { 9 | // cancelFn function// 10 | const cancelFn = function (){ 11 | clearTimeout(timer); 12 | }; 13 | const timer = setTimeout(()=>{ 14 | fn(...args) 15 | }, t); 16 | return cancelFn ; 17 | }; 18 | 19 | 20 | /** 21 | * const result = [] 22 | * 23 | * const fn = (x) => x * 5 24 | * const args = [2], t = 20, cancelT = 50 25 | * 26 | * const log = (...argsArr) => { 27 | * result.push(fn(...argsArr)) 28 | * } 29 | * 30 | * const cancel = cancellable(fn, args, t); 31 | * 32 | * setTimeout(() => { 33 | * cancel() 34 | * console.log(result) // [{"time":20,"returned":10}] 35 | * }, cancelT) 36 | */ -------------------------------------------------------------------------------- /15 - Interval Cancellation.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/interval-cancellation/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var cancellable = function(fn, args, t) { 9 | fn(...args); 10 | 11 | let id = setTimeout(function run() { 12 | fn(...args); 13 | id = setTimeout(run, t); 14 | }, t); 15 | 16 | const cancelFn = () => clearTimeout(id); 17 | 18 | return cancelFn; 19 | }; 20 | 21 | /** 22 | * const result = [] 23 | * 24 | * const fn = (x) => x * 2 25 | * const args = [4], t = 20, cancelT = 110 26 | * 27 | * const log = (...argsArr) => { 28 | * result.push(fn(...argsArr)) 29 | * } 30 | * 31 | * const cancel = cancellable(fn, args, t); 32 | * 33 | * setTimeout(() => { 34 | * cancel() 35 | * console.log(result) // [ 36 | * // {"time":0,"returned":8}, 37 | * // {"time":20,"returned":8}, 38 | * // {"time":40,"returned":8}, 39 | * // {"time":60,"returned":8}, 40 | * // {"time":80,"returned":8}, 41 | * // {"time":100,"returned":8} 42 | * // ] 43 | * }, cancelT) 44 | */ -------------------------------------------------------------------------------- /16 - Promise Time Limit.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/promise-time-limit/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var timeLimit = function(fn, t) { 9 | return async function(...args) { 10 | const originalFnPromise = fn(...args); 11 | 12 | const timeoutPromise = new Promise((_, reject) => { 13 | setTimeout(() => { 14 | reject('Time Limit Exceeded') 15 | }, t); 16 | }) 17 | 18 | return Promise.race([originalFnPromise, timeoutPromise]); 19 | } 20 | }; -------------------------------------------------------------------------------- /17 - Cache With Time Limit.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/cache-with-time-limit/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var TimeLimitedCache = function() { 9 | this.pairs = new Map(); 10 | }; 11 | 12 | /** 13 | * @param {number} key 14 | * @param {number} value 15 | * @param {number} duration time until expiration in ms 16 | * @return {boolean} if un-expired key already existed 17 | */ 18 | TimeLimitedCache.prototype.set = function(key, value, duration) { 19 | const vol = this.pairs.get(key); 20 | const ans = vol && (vol !== -1) ? true : false 21 | this.pairs.set(key, { 22 | val: value, 23 | dur: duration, 24 | index: (vol?.index ?? 0) + 1, 25 | }); 26 | const lll = (duration, ind) => { 27 | setTimeout(() => { 28 | const volIn = this.pairs.get(key); 29 | if (ind === volIn.index) { 30 | this.pairs.set(key, { 31 | ...volIn, 32 | val: -1 33 | }); 34 | return ans; 35 | } 36 | lll(volIn.dur, volIn.index) 37 | }, duration) 38 | } 39 | lll(duration, this.pairs.get(key).index); 40 | return ans; 41 | }; 42 | 43 | /** 44 | * @param {number} key 45 | * @return {number} value associated with key 46 | */ 47 | TimeLimitedCache.prototype.get = function(key) { 48 | return this.pairs.get(key)?.val ?? -1 49 | }; 50 | 51 | /** 52 | * @return {number} count of non-expired keys 53 | */ 54 | TimeLimitedCache.prototype.count = function() { 55 | let sum = 0; 56 | for (let [key, value] of this.pairs) { 57 | if (value.val !== -1) sum++; 58 | } 59 | return sum; 60 | }; 61 | 62 | /** 63 | * Your TimeLimitedCache object will be instantiated and called as such: 64 | * var obj = new TimeLimitedCache() 65 | * obj.set(1, 42, 1000); // false 66 | * obj.get(1) // 42 67 | * obj.count() // 1 68 | */ -------------------------------------------------------------------------------- /18 - Debounce.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/debounce/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var debounce = function(fn, t) { 9 | let timeout; 10 | return function(...args) { 11 | clearTimeout(timeout); 12 | timeout = setTimeout(() => { 13 | fn(...args); 14 | }, t); 15 | } 16 | }; 17 | 18 | /** 19 | * const log = debounce(console.log, 100); 20 | * log('Hello'); // cancelled 21 | * log('Hello'); // cancelled 22 | * log('Hello'); // Logged at t=100ms 23 | */ -------------------------------------------------------------------------------- /19 - Execute Asynchronous Functions in Parallel.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/debounce/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var promiseAll = async function(functions) { 9 | return new Promise((resolve,reject)=>{ 10 | let ans=[],j=functions.length; 11 | functions.forEach((func,i)=>{ 12 | func().then((res)=>{ 13 | (ans[i]=res,--j===0 && resolve(ans)) 14 | }) 15 | .catch(reject) 16 | }) 17 | }) 18 | 19 | }; 20 | 21 | /** 22 | * const promise = promiseAll([() => new Promise(res => res(42))]) 23 | * promise.then(console.log); // [42] 24 | */ -------------------------------------------------------------------------------- /20 - Is Object Empty.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/is-object-empty/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var isEmpty = function(obj) { 9 | return Object.keys(obj).length === 0 ? true : false; 10 | }; -------------------------------------------------------------------------------- /21 - Chunk Array.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/chunk-array/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | const chunk = function(arr, size) { 9 | let full_size = arr.length ; 10 | let resArr = [], temp = []; 11 | 12 | for(let i=0; i< full_size ; i++){ //iterate over arr 13 | temp.push(arr[i]); //push element into the temporary array 14 | 15 | if(temp.length == size){ 16 | resArr.push(temp); 17 | temp = []; //reset array into an empty array// 18 | } 19 | } 20 | 21 | if(temp.length){ // if any elements are left theyre pushed regardless of the size// 22 | resArr.push(temp); 23 | } 24 | 25 | return resArr; 26 | }; -------------------------------------------------------------------------------- /22 - Array Prototype Last.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/array-prototype-last/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | Array.prototype.last = function() { 9 | return this.length ? this[this.length-1] : -1 10 | }; 11 | 12 | /** 13 | * const arr = [1, 2, 3]; 14 | * arr.last(); // 3 15 | */ -------------------------------------------------------------------------------- /23 - Group By.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/group-by/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | Array.prototype.groupBy = function(fn) { 9 | const grouped = {},length = this.length; 10 | for(let i=0;i fn(a) - fn(b)) 10 | }; -------------------------------------------------------------------------------- /25 - Join Two Arrays by ID.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/join-two-arrays-by-id/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var join = function(arr1, arr2) { 9 | const newObj = {}; 10 | const arrs = [...arr1, ...arr2] 11 | for(let index =0; index<=arrs.length-1; index++){ 12 | newObj[arrs[index].id] ={ ...newObj[arrs[index].id], 13 | ...arrs[index]}; 14 | }; 15 | return Object.values(newObj) 16 | }; -------------------------------------------------------------------------------- /26 - Flatten Deeply Nested Array.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/flatten-deeply-nested-array/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | var flat = function (arr, n) { 9 | if (n==0){ 10 | return arr; 11 | } 12 | let ans=[]; 13 | for (let i=0; i0 && Array.isArray(arr[i])){ 15 | ans.push(...flat(arr[i],n-1)); 16 | }else{ 17 | ans.push(arr[i]); 18 | } 19 | } 20 | return ans 21 | }; -------------------------------------------------------------------------------- /27 - Compact Object.js: -------------------------------------------------------------------------------- 1 | // Link in Leetcode 2 | // https://leetcode.com/problems/compact-object/?envType=study-plan-v2&envId=30-days-of-javascript 3 | 4 | 5 | // (----------------------------- THE ANSWER -----------------------------) 6 | 7 | 8 | let getCompact = (val) => { 9 | if (Array.isArray(val)) { 10 | let resultArray = []; 11 | // console.log(`Iterating thru array: ${JSON.stringify(val)}`); 12 | for (let subVal of val) { 13 | let compactSub = getCompact(subVal); 14 | if (Boolean(compactSub)) { 15 | resultArray.push(compactSub); 16 | } 17 | } 18 | return resultArray; 19 | } 20 | 21 | if (typeof val === 'object' && val !== null) { 22 | let resultObject = {}; 23 | // console.log(`Iterating thru object: ${JSON.stringify(val)}`); 24 | for (let [key, subVal] of Object.entries(val)) { 25 | // console.log(`Checking key ${key}: ${JSON.stringify(subVal)}`); 26 | let compactSub = getCompact(subVal); 27 | if (Boolean(compactSub)) { 28 | resultObject[key] = compactSub; 29 | } 30 | } 31 | return resultObject; 32 | } 33 | 34 | if (Boolean(val) === true) return val; 35 | return undefined; 36 | } 37 | 38 | var compactObject = function(obj) { 39 | // console.log(Object.entries(obj)); 40 | return getCompact(obj); 41 | }; --------------------------------------------------------------------------------