├── LICENSE ├── README.md ├── q-1.js ├── q-10.js ├── q-11.js ├── q-12.js ├── q-2.js ├── q-3.js ├── q-4.js ├── q-5.js ├── q-6.js ├── q-7.js ├── q-8.js └── q-9.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Mehul 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solution-leet_code 2 | 3 | 30 Days of JavaScript 4 | 5 | link : https://leetcode.com/studyplan/30-days-of-javascript/ 6 | 7 | Solving all Daily 8 | -------------------------------------------------------------------------------- /q-1.js: -------------------------------------------------------------------------------- 1 | // 2667. Create Hello World Function 2 | 3 | /** 4 | * @return {Function} 5 | */ 6 | var createHelloWorld = function () { 7 | return function (...args) {}; 8 | }; 9 | 10 | var createHelloWorld = function () { 11 | return () => "Hello World"; 12 | }; 13 | /** 14 | * const f = createHelloWorld(); 15 | * f(); // "Hello World" 16 | */ 17 | -------------------------------------------------------------------------------- /q-10.js: -------------------------------------------------------------------------------- 1 | //2619. Array Prototype Last 2 | Array.prototype.last = function () { 3 | return 0 === this.length ? -1 : this[this.length - 1]; 4 | }; 5 | -------------------------------------------------------------------------------- /q-11.js: -------------------------------------------------------------------------------- 1 | // 2621. Sleep 2 | 3 | /** 4 | * @param {number} millis 5 | */ 6 | async function sleep(millis) { 7 | return new Promise((resolve) => { 8 | setTimeout(() => { 9 | resolve(); 10 | }, millis); 11 | }); 12 | } 13 | -------------------------------------------------------------------------------- /q-12.js: -------------------------------------------------------------------------------- 1 | // 2626. Array Reduce Transformation 2 | /** 3 | * @param {number[]} nums 4 | * @param {Function} fn 5 | * @param {number} init 6 | * @return {number} 7 | */ 8 | var reduce = function (nums, fn, init) { 9 | let res = init; 10 | 11 | for (const i of nums) { 12 | res = fn(res, i); 13 | } 14 | 15 | return res; 16 | }; 17 | -------------------------------------------------------------------------------- /q-2.js: -------------------------------------------------------------------------------- 1 | // 2704. To Be Or Not To Be 2 | /** 3 | * @param {string} val 4 | * @return {Object} 5 | */ 6 | var expect = function (val) { 7 | return { 8 | toBe: (expectedValue) => { 9 | if (val === expectedValue) return true; 10 | throw new Error("Not Equal"); 11 | }, 12 | notToBe: (notExpectedValue) => { 13 | if (val !== notExpectedValue) return true; 14 | throw new Error("Equal"); 15 | }, 16 | }; 17 | }; 18 | 19 | /** 20 | * expect(5).toBe(5); // true 21 | * expect(5).notToBe(5); // throws "Equal" 22 | */ 23 | -------------------------------------------------------------------------------- /q-3.js: -------------------------------------------------------------------------------- 1 | // 2634. Filter Elements from Array 2 | 3 | /** 4 | * @param {number[]} arr 5 | * @param {Function} fn 6 | * @return {number[]} 7 | */ 8 | const filter = (arr, fn) => { 9 | const filtered = []; 10 | arr.forEach((elem, index) => { 11 | if (fn(elem, index)) { 12 | filtered.push(elem); 13 | } 14 | }); 15 | return filtered; 16 | }; -------------------------------------------------------------------------------- /q-4.js: -------------------------------------------------------------------------------- 1 | // 2703. Return Length of Arguments Passed 2 | 3 | /** 4 | * @return {number} 5 | */ 6 | function argumentsLength() { 7 | return arguments.length; 8 | } 9 | 10 | /** 11 | * argumentsLength(1, 2, 3); // 3 12 | */ 13 | -------------------------------------------------------------------------------- /q-5.js: -------------------------------------------------------------------------------- 1 | // 2666. Allow One Function Call 2 | 3 | /** 4 | * @param {Function} fn 5 | * @return {Function} 6 | */ 7 | var once = function (fn) { 8 | let called = false; 9 | return function (...args) { 10 | if (!called) { 11 | called = true; 12 | return fn(...args); 13 | } 14 | }; 15 | }; 16 | 17 | /** 18 | * let fn = (a,b,c) => (a + b + c) 19 | * let onceFn = once(fn) 20 | * 21 | * onceFn(1,2,3); // 6 22 | * onceFn(2,3,6); // returns undefined without calling fn 23 | */ 24 | -------------------------------------------------------------------------------- /q-6.js: -------------------------------------------------------------------------------- 1 | // 2723. Add Two Promises 2 | 3 | /** 4 | * @param {Promise} promise1 5 | * @param {Promise} promise2 6 | * @return {Promise} 7 | */ 8 | var addTwoPromises = async function (promise1, promise2) { 9 | let sum = 0; 10 | sum = sum + (await promise1.valueOf()); 11 | sum = sum + (await promise2.valueOf()); 12 | return sum.valueOf(); 13 | }; 14 | 15 | /** 16 | * addTwoPromises(Promise.resolve(2), Promise.resolve(2)) 17 | * .then(console.log); // 4 18 | */ 19 | -------------------------------------------------------------------------------- /q-7.js: -------------------------------------------------------------------------------- 1 | // 2727. Is Object Empty 2 | 3 | /** 4 | * @param {Object | Array} obj 5 | * @return {boolean} 6 | */ 7 | var isEmpty = function (obj) { 8 | return !Object.values(obj).length; 9 | }; 10 | -------------------------------------------------------------------------------- /q-8.js: -------------------------------------------------------------------------------- 1 | // 2724. Sort By 2 | 3 | /** 4 | * @param {Array} arr 5 | * @param {Function} fn 6 | * @return {Array} 7 | */ 8 | var sortBy = (arr, fn) => arr.sort((a, b) => fn(a) - fn(b)); 9 | -------------------------------------------------------------------------------- /q-9.js: -------------------------------------------------------------------------------- 1 | //2722. Join Two Arrays by ID 2 | 3 | /** 4 | * @param {Array} arr1 5 | * @param {Array} arr2 6 | * @return {Array} 7 | */ 8 | var join = function (arr1, arr2) { 9 | arr1.sort((a, b) => a.id - b.id); 10 | arr2.sort((a, b) => a.id - b.id); 11 | const result = []; 12 | let i = 0, 13 | j = 0; 14 | while (i < arr1.length && j < arr2.length) { 15 | const v1 = arr1[i]; 16 | const v2 = arr2[j]; 17 | if (v1.id < v2.id) { 18 | result.push(v1); 19 | i++; 20 | } else if (v1.id > v2.id) { 21 | result.push(v2); 22 | j++; 23 | } else { 24 | result.push({ ...v1, ...v2 }); 25 | i++; 26 | j++; 27 | } 28 | } 29 | while (i < arr1.length) { 30 | result.push(arr1[i]); 31 | i++; 32 | } 33 | while (j < arr2.length) { 34 | result.push(arr2[j]); 35 | j++; 36 | } 37 | return result; 38 | }; 39 | --------------------------------------------------------------------------------