Search⌘ K
AI Features

Versatile Add

Explore how to build a versatile add function in JavaScript that handles multiple calling patterns including chained calls and no-argument invocations. Understand key concepts such as closures, scope, callbacks, and value vs. reference to create a function that returns the correct sum under varying conditions.

We'll cover the following...

Introduction

In this problem, we’ll write a simple function that adds two numbers. The challenge present is that this function will have to be extremely versatile.

This problem will test your understanding of:

  • scope
  • callbacks
  • closures
  • value vs. reference

If you understand these concepts well, this problem can still be extremely difficult, but you have the ability to solve it.

Let’s get started.

Instructions

Write a function that adds 2 numbers. It should work as follows:

add(3, 4); // -> 7
add(10, 12); // -> 22

However, it should also work as follows:

add(3)(4); // -> 7
add(10)(12); // -> 22

BONUS:

Make the following lines of code ...