This is an interesting programming challenge that reddit user claims to have received as . i7_leaf 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 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.” .execute() () I agree with the majority of comments in the discussion thread that the interviewer did not mean to ask this question . As postulated, this function cannot exist. It is for to both be a primitive (the number in the first example) and a function (that accepts 4 as a parameter in the second example). per se impossible addSubtract(1)(2)(3) 0 That said, this is possible with a very slight tweak. While the following two statements be true, the third statement . conceptually cannot both 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 one. practical How does it work? 🤔 Any object can be type cast to a or using built-in methods. string number The use of in the third example attempts to type cast both sides of the argument to a type. If there is no way to type cast to a type, it will attempt to typecast to a type (the same way ). It is because of our ability to explicitly define how to type cast an object that we are able to solve the problem of , and it is because of JavaScript lack of type casting when calling the object by itself that it cannot know that is the primitive . It is, in fact, not the primitive number . It is a function, which is why we can both treat it as an object able to be type cast and call it as a function: + Number Number String 'Hello ' + 123 === 'Hello 123' addSubtract(1)(2)(3) + addSubtract(1)(2)(3)(4)(5)(6) addSubtract(1)(2)(3) 0 0 When treating an object (or function) as a number, the method of that object will be called, the return value of which is what is used for the numerical operation. When treating an object as a string, the method of that object will be called. valueOf toString Let’s curry 🍛 That’s really all the introduction you need to solve this problem, so I’ll provide the solution. I defined the reducer function outside of the main function for readability. You may prefer including it in the function for better encapsulation. The reducer merely alternates addition and subtraction. Given a running total and a new number, if it’s an even index, add; if it’s an odd index, subtract. The recursive function is used to curry parameters. Every function call to just returns , allowing you to call it ad nauseum, each time adding the new parameter to the array of numbers that we will add/subtract. f f f The function has a property. When we type cast to a number, this property will get called. Starting with the first provided number ( ), this property reducers the remaining numbers using the aforementioned alternating operations. f valueOf f x valueOf The first call to then returns the recursive function after having created it. addSubtract f Limitations 🙅 In typical interview fashion, this solution is not perfect. If an interviewer were to grill you about limitations, this has weird behaviors when caching the return value of any of the function calls. Each function call beyond the first call to will be using the same array of . This may cause unintended behavior. addSubtract nums The array is stored inside . The first call adds 2 to the value. The second call subtracts 2 from the value. This can be resolved by returning a new instance of an array for each function call. The original interview question did not stipulate this as a requirement, so I opted to provide the solution with less code complexity. nums addTo1 Conclusion 🔚 I loved the obscurity of this problem, and users seemed to enjoy my solution. I decided I would share out of love for JavaScript language. If you are an interviewer, ask this question during interviews. Make sure your curried examples have the same number of parameters a terminating method. As a JavaScript expert, I do not believe this is a good interview question for JavaScript comprehension. This knowledge makes for a good puzzle, but not for a better developer. If you are an interviewee, do not expect this during an interview. Ask the interviewer for clarification on the different parameter lengths. It was probably a mistake on their part. (If it wasn’t, at least now you know how to solve it!) do not or If you have any questions or relevant insight, please leave a comment. It’s quick, it’s easy, and it’s free! To read more of my columns or contact me, you can find me on and , or . LinkedIn Twitter check out my portfolio on CharlesStover.com