OK, in previous lecture, you have seen how the store functions in variable, how to use function as
a variable and also how to function as a parameter.
The last thing that I want to show you regarding first class functions is how to return function from
other function.
All right.
Let's go to Brackett's.
And in order to demonstrate it, I'm going to use a little example again about exam scores and students.
So using this example, I'm going to check if a student has passed an exam or failed.
And also, according to student score, where he or she would start studying, I mean, start studying
in university or in college.
OK, at first I'm going to create the main function and call it the final result.
So write function, final result.
It will accept one parameter score.
Then in order to define the range we need to use if our statement as a first condition, I'm going to
define its core is greater than or equal to 81 points, if this condition is true, then students will
start studying at university.
I'm going to pass this result in function and then return that function.
So rate return function, this anonymous function accepts one parameter, which will be the name of
the student, the right name inside the parentheses, and then in Calabrese.
Right.
Cancel that log.
Name plus.
You passed an exam and you are a student of, let's say, state university.
OK, after that, we need to define second case, I mean, if score is greater than or equal to 51,
so we need to write else statement with the condition score is greater than or equal to 51.
Then again, we need written statements, the right return function and pass here again, the parameter
name.
So if this condition is true, then suppose that the student will start studying in college, so.
Right, cancel that log name plus.
You passed an exam.
And you are a student of.
State College.
OK, and the last part of the function is Elsah statement in which we will define if a student is failed,
so right else then again return the function with parameter name.
And inside the curly braces, White Castle, that log.
Name plus, you failed.
All right, everything is ready now it's time to call dysfunctions create a variable var, result one.
Equals and right here, the name of the main function, final result with its arguments, let's say
seventy seven points and then let's see in council what we will get right to consider log result one.
Reload and he see that we had a function from else if statement, because this is core, I mean, seventy
seven points is acceptable for the condition of else.
As you see, as a result, we have function here from final result function and we just need to invoke
it at first.
Let's remove this constant log and write result one and pass the argument name as John reload.
And we have John, you passed an exam and you are a student of state college.
OK, let's see another case for other students.
Let's say Jane, create again variable var result two equals and call the function again.
Final result with the arguments in this case, write 44 points and then below write results to add the
name Jane.
Reload and we have Jane, you failed in this case, function from our statement was executed.
All right, we can use a different type of syntax, it is like a shorthand method, we can write final
results, the name of the main function with its argument right here, 96, and then open parentheses
and write the name, for example, Mary.
So it looks like kind of strange, but it works in this way as well, because JavaScript engine executes
code from left to right.
At first it executes final results function, then it reacts to second function and executes it with
proper argument.
So reload.
And we have Mary, you pass an exam and your student of state university.
All right, this was all about first class functions.
See you in the next video.