Photo by Muhammad Haikal Sjukri on Unsplash
Leetcode 30 days of JavaScript (All problems with solutions)
Table of contents
- (1)Create Hello World Function - Leetcode 2667✅
- (2)Counter - Leetcode 2620✅
- (3)Counter 2 - Leetcode 2665✅
- (4)Apply to Transform over each Element in Array - Leetcode 2635✅
- (5)Filter Elements from Array- Leetcode 2634✅
- (6)Array Reduce Transformation (Transforms) - Leetcode 2626✅
- (7)Function Composition - Leetcode 2629✅
- (8)Allow One Function Call - Leetcode 2666 ✅
- (9)Memoize - Leetcode 2623 ✅
- (10)Curry - Leetcode 2632 ✅ ✅
- (11)Sleep - Leetcode 2621✅
- (12)Promise Time Limit - Leetcode 2637 ✅
- (13)Promise Pool - Leetcode 2636 ✅ ✅
- (14)Cache With Time Limit - Leetcode 2622 ✅
- (15)Debounce - Leetcode 2627✅
- (16)Throttle - Leetcode 2676 ✅✅
- (17)JSON Deep Equal - Leetcode 2628
- (18)Convert Object to JSON String - Leetcode 2633
- (19)An Array of Objects to Matrix - Leetcode 2675
- (20)Difference Between Two Objects - Leetcode 2700
- (21)Chunk Array - Leetcode 2677
- (22)Flatten Deeply Nested Array - Leetcode 2625
- (23)Array Prototype Last - Leetcode 2619
- (24)Group By - Leetcode 2631
- (25)Check if Object Instance of Class - Leetcode 2618
- (26)Call Function with Custom Context - Leetcode 2693
- (27)Event Emitter - Leetcode 2694
- (28)Array Wrapper - Leetcode 2695
- (29)Generate Fibonacci Sequence - Leetcode 2648
- (30)Nested Array Generator - Leetcode 2649
- Summary
(1)Create Hello World Function - Leetcode 2667✅
(2)Counter - Leetcode 2620✅
(3)Counter 2 - Leetcode 2665✅
(4)Apply to Transform over each Element in Array - Leetcode 2635✅
(5)Filter Elements from Array- Leetcode 2634✅
(6)Array Reduce Transformation (Transforms) - Leetcode 2626✅
(7)Function Composition - Leetcode 2629✅
(8)Allow One Function Call - Leetcode 2666 ✅
(9)Memoize - Leetcode 2623 ✅
(10)Curry - Leetcode 2632 ✅ ✅
js functions have an attribute length, which specifies how many MINIMUMarguments it can take, we actually know before hand how many array arguments can come
The below code is a beautiful example of the decorator design pattern
We simply accumulate the arguments until they reach the minimum viable args and until then we simply return the SAME function
(11)Sleep - Leetcode 2621✅
-
adding async before the function signature makes it return a promise always and you can call .then() on it always, or use await before it if its within any other async function (we cant use it outside as we don't want to block the main thread)
(12)Promise Time Limit - Leetcode 2637 ✅
-
Async function returns a promise ALWAYS.
(13)Promise Pool - Leetcode 2636 ✅ ✅
(14)Cache With Time Limit - Leetcode 2622 ✅
(15)Debounce - Leetcode 2627✅
"The previous calls" are cancelled if the function is called again and again
The Debounce technique allow us to “group” multiple sequential calls in a single one.
-
When to use debounce? Use debounce to eventually react to a frequent event.
Debounce is useful when you don't need an intermediate state and wish to respond to the end state of the event. That being said, you need to take into account an inevitable delay between the event and the response to it when using debounce.
Common use cases for a debounced function:
Asynchronous search suggestions
Updates batching on the server.
(16)Throttle - Leetcode 2676 ✅✅
Throttling is the action of reducing the number of times a function can be called over time to exactly one.
For example, if we throttle a function by 500ms, it means that it cannot be called more than once per 500ms time frame. Any additional function calls within the specified time interval are simply ignored.
When to use throttle? Use throttling to consistently react to a frequent event.
This technique ensures consistent function execution within a given time interval. Since throttle is bound to a fixed time frame, the event listener should be ready to accept an intermediate state of the event.
Common use cases for throttling include:
Any consistent UI update after window resize
Performance-heavy operations on the server or client.
(17)JSON Deep Equal - Leetcode 2628
- in JS type of array and null is object ! (to check array: Array.isArray())
(18)Convert Object to JSON String - Leetcode 2633
(19)An Array of Objects to Matrix - Leetcode 2675
(20)Difference Between Two Objects - Leetcode 2700
in above example , v is common key among both objects and keys are different , the diff (as per definition of the question ) is [6,7]. Moving on z is also common key , seeing its object (array) index 0 and 1 have same values -so no diff, at index 2 the value is 4 and 3 for other object hence [4,3]. Now for index 3
obj1 :[2,5,6]
obj2:[1]
since 0 is common key "0" :[2,1] is added
(21)Chunk Array - Leetcode 2677
(22)Flatten Deeply Nested Array - Leetcode 2625
only when we see that the depth of arr elem is less than the given value n, we flatten it, we can think of n as the max depth we can go ...
the above thought process and question description both lead to a recursive solution
(23)Array Prototype Last - Leetcode 2619
(24)Group By - Leetcode 2631
(25)Check if Object Instance of Class - Leetcode 2618
every objects root prototype is `Object`, whose own prototype is null
(26)Call Function with Custom Context - Leetcode 2693
(27)Event Emitter - Leetcode 2694
(28)Array Wrapper - Leetcode 2695
(29)Generate Fibonacci Sequence - Leetcode 2648
(30)Nested Array Generator - Leetcode 2649
Summary
Day | Topic | Problem |
1 | Closure | Create Hello World Function |
2 | Closure | Counter |
3 | Closure | Counter II |
4 | Basic Array Transforms | Apply Transform Over Each Element in Array |
5 | Basic Array Transforms | Filter Elements from Array |
6 | Basic Array Transforms | Array Reduce Transformation |
7 | Function input and Output | Function Composition |
8 | Function input and Output | Allow One Function Call |
9 | Function input and Output | Memoize |
10 | Function input and Output | Curry |
11 | Promise | Sleep |
12 | Promise | Promise Time Limit |
13 | Promise | Promise Pool |
14 | Time (setTimeout) | Cache With Time Limit |
15 | Time (setTimeout) | Debounce |
16 | Time (setTimeout) | Throttle |
17 | JSON / Recursion / Important Utilities | JSON Deep Equal |
18 | JSON / Recursion / Important Utilities | Convert Object to JSON String |
19 | JSON / Recursion / Important Utilities | Array of Objects to Matrix |
20 | JSON / Recursion / Important Utilities | Differences Between Two Objects |
21 | JSON / Recursion / Important Utilities | Chunk Array |
22 | JSON / Recursion / Important Utilities | Flatten Deeply Nested Array |
23 | This | Array Prototype Last |
24 | This | Group By |
25 | This | Check if Object Instance of Class |
26 | This | Call Function with Custom Context |
27 | Classes | Event Emitter |
28 | Classes | Array Wrapper |
29 | Generators | Generate Fibonacci Sequence |
30 | Generators | Nested Array Generator |
Note: Type anomalies in Javascript
Arrays Are Objects: In JavaScript, arrays are a specific type of object. This means that arrays inherit properties and methods from the Object prototype, which can sometimes lead to unexpected results when using methods like
hasOwnProperty
.const myArray = [1, 2, 3]; console.log(typeof myArray); // "object" console.log(Array.isArray(myArray)); // true console.log(myArray.hasOwnProperty('0')); // true
While
for...in
iterates over property names,for...of
iterates over property values:const arr = [3, 5, 7]; arr.foo = "hello"; for (const i in arr) { console.log(i);// can access arr[i] } // "0" "1" "2" "foo" for (const i of arr) { console.log(i); } // Logs: 3 5 7 !!!
n JavaScript, arrays are a type of object that can have both indexed elements and additional properties. When you add
foo
as a property to the array, it doesn't become an indexed element but rather an additional property.The
for...of
loop iterates over the indexed elements of an array, not its properties. Therefore, it only iterates over the values3
,5
, and7
and ignores the propertyfoo
NaN: "NaN" stands for "Not-a-Number," but it is of type "number." This behavior can lead to confusing results when dealing with mathematical operations.
const result = "Hello" / 2; // NaN console.log(typeof result); // "number"
Type Coercion: JavaScript performs type coercion, which can automatically convert values between types when operators or functions expect a different type.
const sum = 5 + "5"; // "55" (string) console.log(typeof sum); // "string"