@jgCarroll/

curryFunction

JavaScript

A common interview question.

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
32
33
34
35
36
37
38
39
// // starter code

// function getProduct(num1, num2) {
//   return num1 * num2
// }

// getProduct(10, 20)

// =================================
// solution

function getProduct(num1) {
  return function(num2) {
    return num1 * num2
  }
}

getProduct(10)(20)

// =================================
// another example (using kilometers and KPH)

function getTravelTime(distance, speed) {
  return distance / speed
}

getTravelTime(600, 50)

// re-written as a curried function with the distance set to that between Boston and NYC

function getTravelTime(distance) {
  return function(speed) {
    return distance / speed
  }
}

const travelTimeBosNyc = getTravelTime(400)

console.log(travelTimeBosNyc(100))
Native Browser JavaScript