Function call in function arguments
We continue the theme of expressions. As you remember, a function call is an expression, which means we can put a function call into a function call (… function call, a function call into a call … aaaa!).
var result = Math.min(1, Math.min(2, 3));
We call the function
Math.min and pass two arguments to it:
the result of calling the function
You can do the same, but with intermediate steps:
var secondNumber = Math.min(2, 3); var result = Math.min(1, secondNumber);
Which option is preferable? If the calculation is quite simple and shallow (no more than one function embedding), then you can safely put the call into the call. In other situations, it is preferable to split the calls into intermediate calculations.
The reasons are the same. Reading this code is much easier. First, intermediate variables by their names reflect the essence of operations. Secondly, such code is easier to debug, and intermediate data is easier to explore. And thirdly, deep nested challenges are difficult to read.
To build the family tree of the Starks family, Sam wrote the
parentFor function, which returns the name of the parent if the first parameter is baby’s full name. The second parameter function takes the line
"mother". So the function understands which parent to return.
Write a program that displays the name grandfather Joffrey on the maternal line. Full name of Joffrey:
Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on GitHub.