The first time to write an article in this above if there is anything to write bad place, I hope you will bear with me, you can get your knowledge below.
add(1)(2)(3)
Friends, do not know if you have encountered this topic in the process of interview? The interviewer just gave us add(1), (2) and (3) and asked us to implement it. Here is my simple understanding of the problem.
Solution a:
var add = function(a){
return function(b){
return function(c){
return a+b+c;
}
}
}
console.log(add(1)(2)(3);
Copy the code
Method 2:
const add = x => y => z => x+y+z;
console.log(add(1)(2)(3);
Copy the code
Use the arrow function that appears in ES6 and if there is only one line of code that is the return value of this method, you can define the variable without curly braces and const. So that’s actually the correct answer to this question, so what does the interviewer want to test us on,
- 1. I think the first one is our simple understanding of function calls,
- 2. The second is what you know about Corrification,
- 3. And how well you know ES6
His observation of the es6 aspect, and of the Currization operation, is also not bad if you think it’s an easy problem at first, and you write the above answer. If you write one of the following directly, the interviewer will feel that you have more knowledge than just using native JS to solve problems.
Implement add(1,2)(3) and add(1)(2,3)
Here are the questions that I think can be asked about the Coriolization operation.
How realistic? If you start with the first solution, you should probably write it like this:
var add = function(a,b){ return function(c){ return a+b+c; }} to the console. The log (add (1, 2) (3)); Var add1 = function(a){return function(b,c){return a+b+c}} console.log(add1(1)(2,3));Copy the code
If we want to use the idea of Cremation in solution two to solve this problem. My understanding is that we’re going to implement a utility function specifically to generate currified functions.
The main idea is to determine whether the number of parameters (args. Length) of the current function is greater than or equal to the number of parameters required by the original function (fn.length), if so, then execute the current function; If it is less than, a function is returned. Here is the function implementation:
const curry = (fn, ... Args)=> // The number of arguments to the function can be accessed directly through the.length property of the function args. Length >=fn. // If the number of arguments passed is greater than or equal to the number of arguments to fn of the original function, the function is executed directly. fn(... Args) // When the number of arguments passed in the betelgeuse is less than the number of arguments in the fn of the original function // The current function continues to be currized, returning a function that takes all arguments (current and remaining arguments) : (... _args)=> curry(fn, ... args, ... _args); function add1(x,y,z){ return x+y+z; } const add = curry(add1) console.log(add(1,2,3)); console.log(add(1)(2)(3)); The console. The log (add (1, 2) (3)); console.log(add(1)(2)(3));Copy the code
conclusion
There are two solutions derived from the question add(1), (2) and (3) above, both of which are very good. However, I prefer the second solution. After all, you should show your strengths and outstanding points during the interview, so that you can stand out from the crowd.
If you have any questions in the above expression, please bear with me. Of course, you can also point out my mistakes in the comments section below and share with me any more advanced solutions to this problem. Hopefully this solution will help you in a future interview.
Thank you for reading, and finally if you feel that our article is of any help to you, please use your rich little hands to give me a thumbs up