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 Calculator Implementation

Create a chainable calculator API that supports method chaining for arithmetic operations with a fluent interface pattern.

Here's an implementation of a Chain Calculator in JavaScript. This calculator allows chaining operations like addition, subtraction, multiplication, and division in a fluent and readable way. -- βœ… Implementation Explanation 1. Constructor : Takes an initial value (default is ). 1. Methods : : Adds the given number to the current value. : Subtracts the given number from the current value. : Multiplies the current value by the given number. : Divides the current value by the given number, with a check to prevent division by zero. : Returns the current value. : Resets the value to (or any initial value). 1. Chaining : Each method returns the instance (), enabling method chaining. Try It You can use this pattern to build more complex calculators by adding operations or features like exponentiation, modulus, or even memory storage. Runkit link -- <!-quiz-start --Q1: What enables method chaining in the ChainCalculator class? [ ] Using arrow functions for all methods [ ] Storing values in an array [x] Returning from each operation method [ ] Using the constructor to initialize methods Q2: What is the output of: ? [ ] 25 [x] 30 [ ] 20 [ ] 35 Q3: What happens when you call on the ChainCalculator? [ ] Returns Infinity [ ] Returns 0 [ ] Returns NaN [x] Throws an Error with message "Division by zero is not allowed." <!-quiz-end --
Machine Coding
πŸ“Š Analytics SDK with Retry Logic
medium
🧭 React Breadcrumb Component
medium
πŸ”’ Chained Calculator Implementation
medium
πŸ–±οΈ Cursor Tracking: Position Monitoring & Interactive Effects
hard
πŸ”’ React Pagination with Ellipsis
hard
πŸ“Š Progress Bar with Controls
medium
3 of 6
LibraryMachine Coding3 of 6

πŸ”’ Chained Calculator Implementation

machine-codingmedium

Here's an implementation of a Chain Calculator in JavaScript. This calculator allows chaining operations like addition, subtraction, multiplication, and division in a fluent and readable way.


βœ… Implementation

class ChainCalculator {
  constructor(initialValue = 0) {
    this.value = initialValue;
  }

  add(number) {
    this.value += number;
    return this; // Return `this` to enable chaining
  }

  subtract(number) {
    this.value -= number;
    return this;
  }

  multiply(number) {
    this.value *= number;
    return this;
  }

  divide(number) {
    if (number === 0) {
      throw new Error("Division by zero is not allowed.");
    }
    this.value /= number;
    return this;
  }

  getResult() {
    return this.value;
  }

  reset() {
    this.value = 0;
    return this;
  }
}

// Example usage:
const calculator = new ChainCalculator(10);

const result = calculator
  .add(5)     // 10 + 5 = 15
  .subtract(3) // 15 - 3 = 12
  .multiply(4) // 12 * 4 = 48
  .divide(2)   // 48 / 2 = 24
  .getResult();

console.log(result); // Output: 24

Explanation

  1. Constructor :
  • Takes an initial value (default is 0).
  1. Methods :
  • add(number): Adds the given number to the current value.
  • subtract(number): Subtracts the given number from the current value.
  • multiply(number): Multiplies the current value by the given number.
  • divide(number): Divides the current value by the given number, with a check to prevent division by zero.
  • getResult(): Returns the current value.
  • reset(): Resets the value to 0 (or any initial value).
  1. Chaining :
  • Each method returns the instance (this), enabling method chaining.

Try It

You can use this pattern to build more complex calculators by adding operations or features like exponentiation, modulus, or even memory storage.

Runkit link


Quick Quiz

Test your understanding with 3 quick questions

Q1What enables method chaining in the ChainCalculator class?
Q2What is the output of: `new ChainCalculator(10).add(5).multiply(2).getResult()`?
Q3What happens when you call `divide(0)` on the ChainCalculator?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna