1
00:00:02,090 --> 00:00:08,900
By the way a place where this would have helped us is in our app from module two, from the basics module

2
00:00:09,350 --> 00:00:14,360
where we also have a couple of add listeners for adding, subtracting, multiplying and so on and we have

3
00:00:14,360 --> 00:00:21,470
to bind them to different functions here because whilst a lot of code is the same, we have some differences

4
00:00:21,470 --> 00:00:25,840
here regarding the operation we perform and what we write to a log.

5
00:00:25,850 --> 00:00:32,480
Now with if statements and dynamically bound values here, we could have merged this all into one function

6
00:00:32,480 --> 00:00:34,080
which I quickly want to show you.

7
00:00:34,370 --> 00:00:37,250
Here we could add a calculate function

8
00:00:37,250 --> 00:00:41,810
and again you can use either function syntax, since I always use the function declaration syntax in this

9
00:00:41,810 --> 00:00:42,290
file here,

10
00:00:42,290 --> 00:00:52,290
I'll stick to it and there you could actually expect a value, let's say an operation parameter again, where

11
00:00:52,290 --> 00:00:57,520
you check if operation is equal to add and you can use global constants of course

12
00:00:57,690 --> 00:01:06,360
and if that is the case, then what you do is you perform this calculation,

13
00:01:06,410 --> 00:01:12,100
else if operation is equal to subtract and you could use a switch case statement

14
00:01:12,110 --> 00:01:15,960
in this example too by the way, you would do the same. Here

15
00:01:15,980 --> 00:01:17,480
I see I copied the wrong code,

16
00:01:17,480 --> 00:01:20,580
of course I want to copy the code from add here, so

17
00:01:20,690 --> 00:01:22,330
let me do that,

18
00:01:22,410 --> 00:01:24,240
that will be replaced here.

19
00:01:24,350 --> 00:01:28,200
So we have the add code, we have the subtract code here,

20
00:01:28,240 --> 00:01:32,320
else if operation is equal to multiply,

21
00:01:33,250 --> 00:01:41,150
if that's the case, then we grab this multiplication code here, like this and in the else block, that

22
00:01:41,150 --> 00:01:49,730
has to be the division, we grab that division code here, like that and now let's just grab that code which

23
00:01:49,730 --> 00:01:56,110
is always the same where we get the entered number and the initial result, like this and now we have

24
00:01:56,110 --> 00:01:57,270
this all in one function.

25
00:01:57,280 --> 00:02:07,170
We can even shorten this a bit more by adding a new variable operator which we then set to a plus here

26
00:02:07,860 --> 00:02:15,810
and which we set to a minus here and which we set to times here

27
00:02:16,960 --> 00:02:25,040
and which we set to this divide operator here and that allows us to basically grab all the create and

28
00:02:25,040 --> 00:02:29,990
write output calls here and write to log calls and remove them from there,

29
00:02:29,990 --> 00:02:37,760
move that out of the else if block and here, use the operator variable we just worked on, here we can use

30
00:02:37,760 --> 00:02:43,890
the operation which I expect to get as an argument because that should be add, subtract, multiply and so

31
00:02:43,890 --> 00:02:49,320
on and therefore now, we have this all in one function which we now just have to bind down there,

32
00:02:49,320 --> 00:02:54,300
so we always call calculate but we just have to make sure that the different value is passed in for

33
00:02:54,300 --> 00:02:57,480
operation and that's now super easy to do,

34
00:02:57,540 --> 00:03:02,090
we can just call bind here, this, again ignoring what this does

35
00:03:02,280 --> 00:03:05,610
and then here we pass add and so on.

36
00:03:05,610 --> 00:03:08,130
So down there, we pass in

37
00:03:08,130 --> 00:03:10,280
subtract,

38
00:03:10,530 --> 00:03:22,870
here we pass in multiply and here we pass in divide and now we can actually delete all the other functions

39
00:03:22,870 --> 00:03:23,070
here,

40
00:03:23,080 --> 00:03:26,060
so add, subtract and so on and just go with calculate

41
00:03:26,290 --> 00:03:34,830
and if we save that and we reload, we'll have a calculator that works exactly as before but now with

42
00:03:34,830 --> 00:03:37,180
the help of bind and if statements.

43
00:03:37,200 --> 00:03:42,780
So this is now a way more concise way of writing this and it hopefully also shows why this might be useful

44
00:03:42,780 --> 00:03:44,600
to have, why this might be nice to have.
