@jgCarroll/

tripleAddFunction

JavaScript

A common interview question/problem.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function tripleAdd (num1) {
  return function (num2) {
    return function (num3) {
      return num1 + num2 + num3
    }
  }
}

tripleAdd(10)(20)(30) // returns total of all 3 numbers added together

// tripleAdd is known as a curried function since it is a function that returns other functions
// ==================================
// An example of a single function producing the same result as above

function tripleAdd1 (num1, num2, num3) {
  return num1 + num2 + num3
}

tripleAdd1(10, 20, 30)

// ===================================

function quadrupleAdd(num1) {
  return function(num2) {
    return function(num3, num4) {
      return num1 + num2 + num3 + num4;
    };
  };
}
 
quadrupleAdd(10)(20)(30, 40)
Native Browser JavaScript