WEBVTT

00:01.230 --> 00:07.800
OK, in previous lecture, you have seen how the store functions in variable, how to use function as

00:07.800 --> 00:11.130
a variable and also how to function as a parameter.

00:12.000 --> 00:17.640
The last thing that I want to show you regarding first class functions is how to return function from

00:17.640 --> 00:18.330
other function.

00:19.380 --> 00:19.830
All right.

00:20.190 --> 00:21.330
Let's go to Brackett's.

00:21.720 --> 00:28.290
And in order to demonstrate it, I'm going to use a little example again about exam scores and students.

00:28.860 --> 00:34.340
So using this example, I'm going to check if a student has passed an exam or failed.

00:34.680 --> 00:41.370
And also, according to student score, where he or she would start studying, I mean, start studying

00:41.370 --> 00:43.290
in university or in college.

00:43.860 --> 00:48.960
OK, at first I'm going to create the main function and call it the final result.

00:49.110 --> 00:51.510
So write function, final result.

00:54.010 --> 00:57.190
It will accept one parameter score.

00:59.420 --> 01:06.320
Then in order to define the range we need to use if our statement as a first condition, I'm going to

01:06.320 --> 01:16.160
define its core is greater than or equal to 81 points, if this condition is true, then students will

01:16.160 --> 01:18.040
start studying at university.

01:18.530 --> 01:22.490
I'm going to pass this result in function and then return that function.

01:22.940 --> 01:31.430
So rate return function, this anonymous function accepts one parameter, which will be the name of

01:31.430 --> 01:36.650
the student, the right name inside the parentheses, and then in Calabrese.

01:36.730 --> 01:36.980
Right.

01:36.980 --> 01:37.850
Cancel that log.

01:40.330 --> 01:41.380
Name plus.

01:43.860 --> 01:54.300
You passed an exam and you are a student of, let's say, state university.

01:58.070 --> 02:06.380
OK, after that, we need to define second case, I mean, if score is greater than or equal to 51,

02:07.040 --> 02:15.020
so we need to write else statement with the condition score is greater than or equal to 51.

02:18.420 --> 02:28.410
Then again, we need written statements, the right return function and pass here again, the parameter

02:28.830 --> 02:29.250
name.

02:31.440 --> 02:38.370
So if this condition is true, then suppose that the student will start studying in college, so.

02:38.370 --> 02:43.020
Right, cancel that log name plus.

02:45.190 --> 02:46.660
You passed an exam.

02:50.120 --> 02:53.060
And you are a student of.

02:55.070 --> 02:56.360
State College.

02:59.940 --> 03:07.500
OK, and the last part of the function is Elsah statement in which we will define if a student is failed,

03:07.890 --> 03:16.470
so right else then again return the function with parameter name.

03:20.100 --> 03:23.220
And inside the curly braces, White Castle, that log.

03:27.500 --> 03:30.410
Name plus, you failed.

03:34.040 --> 03:43.280
All right, everything is ready now it's time to call dysfunctions create a variable var, result one.

03:45.660 --> 03:54.150
Equals and right here, the name of the main function, final result with its arguments, let's say

03:54.450 --> 04:02.790
seventy seven points and then let's see in council what we will get right to consider log result one.

04:06.490 --> 04:14.050
Reload and he see that we had a function from else if statement, because this is core, I mean, seventy

04:14.050 --> 04:17.890
seven points is acceptable for the condition of else.

04:19.630 --> 04:26.140
As you see, as a result, we have function here from final result function and we just need to invoke

04:26.140 --> 04:27.610
it at first.

04:27.970 --> 04:41.410
Let's remove this constant log and write result one and pass the argument name as John reload.

04:42.550 --> 04:47.500
And we have John, you passed an exam and you are a student of state college.

04:48.760 --> 04:52.840
OK, let's see another case for other students.

04:52.840 --> 05:03.070
Let's say Jane, create again variable var result two equals and call the function again.

05:03.070 --> 05:16.900
Final result with the arguments in this case, write 44 points and then below write results to add the

05:16.900 --> 05:17.590
name Jane.

05:20.340 --> 05:27.870
Reload and we have Jane, you failed in this case, function from our statement was executed.

05:29.420 --> 05:38.780
All right, we can use a different type of syntax, it is like a shorthand method, we can write final

05:38.780 --> 05:49.010
results, the name of the main function with its argument right here, 96, and then open parentheses

05:49.940 --> 05:52.820
and write the name, for example, Mary.

05:55.660 --> 06:03.160
So it looks like kind of strange, but it works in this way as well, because JavaScript engine executes

06:03.160 --> 06:05.200
code from left to right.

06:05.740 --> 06:12.730
At first it executes final results function, then it reacts to second function and executes it with

06:12.730 --> 06:13.800
proper argument.

06:14.470 --> 06:15.460
So reload.

06:16.390 --> 06:20.890
And we have Mary, you pass an exam and your student of state university.

06:23.070 --> 06:27.270
All right, this was all about first class functions.

06:27.840 --> 06:29.010
See you in the next video.
