Variable length currying in JavaScript

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:

How does it work? 🤔

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

Let’s curry 🍛

That’s really all the introduction you need to solve this problem, so I’ll provide the solution.

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 addSubtract will be using the same array of nums. This may cause unintended behavior.

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, do not ask this question during interviews. Make sure your curried examples have the same number of parameters or 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!)

Senior front end engineer /

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store