Variable length currying in JavaScript

Charles Stover
4 min readNov 19, 2018

This is an interesting programming challenge that reddit user i7_leaf claims to have received as an interview question.

Preface ⭐

There is one key difference between the original question and what I will cover in this article. The interview question asked the candidate to write a function that executes as follows:

It is worth noting that this curried function does not end in any sort of delimiter, e.g. a terminating method .execute() or empty parameter (). What makes this challenge both difficult and interesting is the lack of signal that “this is the last digit in the sequence.”

I agree with the majority of comments in the discussion thread that the interviewer did not mean to ask this question per se. As postulated, this function cannot exist. It is impossible for addSubtract(1)(2)(3) to both be a primitive (the number 0 in the first example) and a function (that accepts 4 as a parameter in the second example).

That said, this is conceptually possible with a very slight tweak. While the following two statements cannot both be true, the third statement can.

I believe the interviewer was actually testing the candidate’s ability to write a curried function that alternates add and subtract operations, but innocently did not realize the two function examples were mutually exclusive. The scenario in the third statement is such an obscure functionality of JavaScript that I do not support its use as an interview question. It’s a “think outside the box” solution, but not a practical one.

How does it work? 🤔

Any object can be type cast to a string or number using built-in methods.