hiteshchoudhary / open-source-contribution

A repo to contribute in open source via README only. A dream repo for open source beginner
245 stars 393 forks source link

Add Advance JavaScript Concepts that will ease beginners' life #426

Open Dheeraj-Tiwari opened 4 days ago

Dheeraj-Tiwari commented 4 days ago

Title

Advanced JavaScript Concepts: Memoization, Function Currying, and Reduce Use Cases

Description

Summary

✨ This pull request adds a comprehensive guide on advanced JavaScript concepts crucial for freshers during interviews and for daily development tasks. The concepts covered include:

  1. Memoization
  2. Function Currying
  3. Reduce Use Cases

Each concept is explained with detailed theory and practical examples to help developers understand and apply them effectively.


Details

πŸ“Œ Memoization

Memoization is an optimization technique used to speed up function calls by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

Example:

function memoize(fn) {
  const cache = new Map();
  return function(...args) {
    const key = JSON.stringify(args);
    if (cache.has(key)) {
      return cache.get(key);
    }
    const result = fn(...args);
    cache.set(key, result);
    return result;
  };
}

const add = (a, b) => a + b;
const memoizedAdd = memoize(add);

console.log(memoizedAdd(1, 2)); // 3
console.log(memoizedAdd(1, 2)); // 3, retrieved from cache

Memoization Example


πŸ“Œ Function Currying

Currying is a technique in functional programming where a function is transformed into a sequence of functions, each taking a single argument.

Example:

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn.apply(this, args);
    } else {
      return function(...nextArgs) {
        return curried.apply(this, args.concat(nextArgs));
      };
    }
  };
}

function multiply(a, b) {
  return a * b;
}

const curriedMultiply = curry(multiply);

console.log(curriedMultiply(2)(3)); // 6

Function Currying Example


πŸ“Œ Reduce Use Cases

The reduce method executes a reducer function on each element of the array, resulting in a single output value. It is a powerful method for performing complex operations on arrays.

Example:

const numbers = [1, 2, 3, 4, 5];

const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 15

const product = numbers.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
console.log(product); // 120

Reduce Use Cases Example


πŸ§ͺ Testing

These examples have been tested in multiple environments to ensure compatibility and correctness. They are designed to be easily understandable and applicable to real-world scenarios.


Additional Context

πŸ“š This addition aims to provide freshers and experienced developers with the knowledge and tools needed to excel in interviews and daily development tasks. These concepts are fundamental in JavaScript and are often asked in technical interviews.


Please review the additions and provide feedback. Looking forward to contributing more to this amazing repository!