Leetcode 30 days of JavaScript (All problems with solutions)

(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

DayTopicProblem
1ClosureCreate Hello World Function
2ClosureCounter
3ClosureCounter II
4Basic Array TransformsApply Transform Over Each Element in Array
5Basic Array TransformsFilter Elements from Array
6Basic Array TransformsArray Reduce Transformation
7Function input and OutputFunction Composition
8Function input and OutputAllow One Function Call
9Function input and OutputMemoize
10Function input and OutputCurry
11PromiseSleep
12PromisePromise Time Limit
13PromisePromise Pool
14Time (setTimeout)Cache With Time Limit
15Time (setTimeout)Debounce
16Time (setTimeout)Throttle
17JSON / Recursion / Important UtilitiesJSON Deep Equal
18JSON / Recursion / Important UtilitiesConvert Object to JSON String
19JSON / Recursion / Important UtilitiesArray of Objects to Matrix
20JSON / Recursion / Important UtilitiesDifferences Between Two Objects
21JSON / Recursion / Important UtilitiesChunk Array
22JSON / Recursion / Important UtilitiesFlatten Deeply Nested Array
23ThisArray Prototype Last
24ThisGroup By
25ThisCheck if Object Instance of Class
26ThisCall Function with Custom Context
27ClassesEvent Emitter
28ClassesArray Wrapper
29GeneratorsGenerate Fibonacci Sequence
30GeneratorsNested Array Generator

Note: Type anomalies in Javascript

  1. 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 values 3, 5, and 7 and ignores the property foo

  2. 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"
    
  3. 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"