@jgCarroll/

singleAndDoubleInvokedFunctions

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
// function called either with multiple arguments, or in a curried manner

function getTotal() {
    var args = Array.prototype.slice.call(arguments)
    if (args.length === 2) {
      return args[0] + args[1]
    } else if (args.length === 1) {
      return function(num2) {
        return args[0] + num2
      }
    }
}
 
console.log(getTotal(10, 20));
console.log(getTotal(5, 40));

console.log(getTotal(10)(20));
console.log(getTotal(8)(12));
Native Browser JavaScript