CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

➕ Chained Sum (Curried Function)

Curried sum function enabling chained calls like sum(1)(2)(3). Demonstrates closures, internal state, and valueOf/toString methods.

Here are two different implementations of a curried sum function, one using state within the function and the other using recursion. -- ✅ Currying with Internal State Explanation: Internal state tracking : The property is used to store the running total. Chaining : The function returns itself () to allow for chaining additional calls. and : These methods are added to ensure that the function behaves like a number in comparisons and string conversions. 2. Currying with Recursion : Explanation: Recursion : The function calls itself () to accumulate the sum as more values are provided. Base case : The base case for the sum is the initial value passed to the function. and : These methods are used to convert the recursive function to a primitive value for comparisons and string conversions. Key Differences: 1. State Management : The first implementation uses internal state () to track the running total. The second implementation uses recursion to accumulate the sum by calling . 1. Performance : The first implementation is more straightforward and has a stateful function that can be chained directly. The second implementation, using recursion, creates new function instances on each call, which might have slight overhead in deep recursion cases. Both implementations allow you to chain function calls to accumulate the sum, with the key distinction being whether you manage state via a variable () or via recursive function calls. -- <!-quiz-start --Q1: Why is the method added to the inner function in a chained sum implementation? [ ] To allow the function to be called with multiple arguments [x] To make the function evaluate to a number when used in comparisons [ ] To prevent memory leaks in the closure [ ] To enable recursion in the function Q2: What is the key difference between the state-based and recursive implementations of chained sum? [ ] Only the recursive version supports chaining [ ] The state-based version is always faster [x] State-based uses a property to track total, recursive creates new function instances [ ] Only the state-based version can handle negative numbers Q3: What would return if was not implemented? [ ] true [x] false [ ] It would throw an error [ ] undefined <!-quiz-end --
JavaScriptUtilities
➕ Chained Sum (Curried Function)
medium
⏱️ Debounce Function in JavaScript
medium
📋 Deep Clone Implementation
easy
🔄 distinctUntilChanged() Polyfill
easy
📄 Document Comparison (Diff)
easy
📢 Custom EventEmitter Implementation
hard
📦 Flatten Object Implementation
medium
🐫➡️🐍 Converting camelCase to snake_case in JavaScript (Without Regex)
easy
🔄 mapLimit: Controlled Concurrency in JavaScript
medium
⚡️ Fire on Push: Dispatching Custom Events When an Array Changes in JavaScript
medium
🔄 Removing Circular References from Objects
hard
📊 Sampling Function: Execute Once Every N Calls
medium
⏱️ Throttle Function in JavaScript
medium
🔄 undefinedToNull Utility
medium
1 of 14
LibraryJavaScriptUtilities48 of 61

➕ Chained Sum (Curried Function)

jsutilsmedium

Here are two different implementations of a curried sum function, one using state within the function and the other using recursion.


✅ Currying with Internal State

function sum(a) {
  // Create a new function that maintains its own state
  function innerSum(b) {
    // Track the running total as a property of the function itself
    if (b === undefined) {
      return innerSum.total;
    }

    // Add the new value to our running total
    innerSum.total += b;

    // Return this function again to allow for chaining
    return innerSum;
  }

  // Initialize the total
  innerSum.total = a;

  // Add a valueOf method to make the function evaluate to the total when used in comparisons
  innerSum.valueOf = function() {
    return innerSum.total;
  };

  // Add a toString method to make the function convert to the total when used as a string
  innerSum.toString = function() {
    return innerSum.total.toString();
  };

  return innerSum;
}

// Test cases
const sum1 = sum(1);
console.log(sum1(2) == 3);  // true
console.log(sum1(3) == 6);  // true
console.log(sum(1)(2)(3) == 6);  // true
console.log(sum(5)(-1)(2) == 6);  // true

Explanation:

  • Internal state tracking : The innerSum.total property is used to store the running total.
  • Chaining : The function returns itself (innerSum) to allow for chaining additional calls.
  • valueOf and toString : These methods are added to ensure that the function behaves like a number in comparisons and string conversions.

2. Currying with Recursion :

function sum(a) {
  // Base case value
  const currentSum = a;

  // Return a new function that will handle the next value
  function nextSum(b) {
    // When called with a new value, create a new sum starting
    // with the current accumulated value plus the new value
    return sum(currentSum + b);
  }

  // Add valueOf to allow for comparison with primitives
  nextSum.valueOf = function() {
    return currentSum;
  };

  // Add toString for string conversions
  nextSum.toString = function() {
    return currentSum.toString();
  };

  return nextSum;
}

// Test cases
const sum1 = sum(1);
console.log(sum1(2) == 3);  // true
console.log(sum1(3) == 4);  // true
console.log(sum(1)(2)(3) == 6);  // true
console.log(sum(5)(-1)(2) == 6);  // true

Explanation:

  • Recursion : The function calls itself (sum(currentSum + b)) to accumulate the sum as more values are provided.
  • Base case : The base case for the sum is the initial value passed to the function.
  • valueOf and toString : These methods are used to convert the recursive function to a primitive value for comparisons and string conversions.

Key Differences:

  1. State Management :
  • The first implementation uses internal state (innerSum.total) to track the running total.
  • The second implementation uses recursion to accumulate the sum by calling sum(currentSum + b).
  1. Performance :
  • The first implementation is more straightforward and has a stateful function that can be chained directly.
  • The second implementation, using recursion, creates new function instances on each call, which might have slight overhead in deep recursion cases.

Both implementations allow you to chain function calls to accumulate the sum, with the key distinction being whether you manage state via a variable (total) or via recursive function calls.


Quick Quiz

Test your understanding with 3 quick questions

Q1Why is the `valueOf` method added to the inner function in a chained sum implementation?
Q2What is the key difference between the state-based and recursive implementations of chained sum?
Q3What would `sum(1)(2)(3) == 6` return if `valueOf` was not implemented?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna