1
00:00:02,310 --> 00:00:08,760
For this, I want to reuse the calculator and attached you will find my version of that calculator,

2
00:00:08,790 --> 00:00:13,920
the one we finished in the basics module, you can of course also use yours or just download and use mine

3
00:00:13,920 --> 00:00:15,810
to have exactly the same code

4
00:00:15,980 --> 00:00:22,830
and in there, I want to give you an example of how we could use conditional expressions to optimize

5
00:00:22,830 --> 00:00:28,020
this code a bit more because of course, this works without any if statements, clearly because we haven't

6
00:00:28,020 --> 00:00:34,540
learned about them yet but you might notice that in the add, subtract, multiply and divide functions,

7
00:00:34,620 --> 00:00:36,930
we have a lot of code duplication.

8
00:00:36,930 --> 00:00:39,290
We always start by getting the entered number,

9
00:00:39,300 --> 00:00:40,930
then we set the initial result,

10
00:00:40,950 --> 00:00:43,490
that's always exactly the same,

11
00:00:43,530 --> 00:00:51,390
then we have our calculation which is always different, then however we again just write the output or

12
00:00:51,390 --> 00:00:52,710
write to the log

13
00:00:52,710 --> 00:00:57,870
and yes, we do pass different information in there but actually the only information that is different

14
00:00:58,110 --> 00:01:02,460
is the operator that we used and description of the operation we did,

15
00:01:02,580 --> 00:01:05,360
the other parameters are always the same.

16
00:01:05,400 --> 00:01:11,820
So there is a lot of code reusage but without conditional statements, without if statements, there

17
00:01:11,820 --> 00:01:13,650
really wasn't a way around it,

18
00:01:13,650 --> 00:01:15,630
well now this changes.

19
00:01:15,630 --> 00:01:20,820
Now we can for example add a new function here, maybe just name it

20
00:01:20,820 --> 00:01:31,150
calculate result, something like this and there, I'll bring in my code that I have in the add function

21
00:01:31,930 --> 00:01:35,070
but here I now want to write this in a more flexible way,

22
00:01:35,320 --> 00:01:48,050
here I can now expect a parameter, let's say calculation type and I expect this to be a string that describes

23
00:01:48,050 --> 00:01:50,790
the type of calculation we want to perform.

24
00:01:50,810 --> 00:01:57,800
Now we can add an if statement here by just adding if, this is a keyword understood by Javascript, then

25
00:01:57,800 --> 00:02:02,090
parentheses where we place our condition in and then important,

26
00:02:02,090 --> 00:02:04,030
curly braces.

27
00:02:04,130 --> 00:02:08,260
Now this here, the parentheses here, that will take our condition,

28
00:02:08,420 --> 00:02:15,110
the parentheses here will in the end take our body, our code that should be executed if the condition

29
00:02:15,110 --> 00:02:15,830
is met.

30
00:02:15,830 --> 00:02:18,980
Now you don't add a semicolon after this,

31
00:02:18,980 --> 00:02:24,360
just as you didn't do it for the function here and now we need to add a condition here.

32
00:02:24,410 --> 00:02:33,400
Now the condition could be that calculation type is equal, with the triple equality operator here, equal

33
00:02:33,400 --> 00:02:43,900
to let's say add. If that is the case, then I want to set current result equal to current result plus entered

34
00:02:43,900 --> 00:02:44,960
number

35
00:02:45,430 --> 00:02:51,280
and now I also want to provide an alternative to that and we do that with the else keyword.

36
00:02:51,280 --> 00:02:58,040
Now you add that right after your closing curly brace for the if blocks, so the

37
00:02:58,330 --> 00:02:59,560
if the condition is met

38
00:02:59,560 --> 00:03:05,740
block and then you add another pair of curly braces that now marks the block of code that should be

39
00:03:05,740 --> 00:03:08,400
executed if this condition is not met.

40
00:03:08,410 --> 00:03:13,330
So if the condition is met, we make it into this first block and therefore this code gets executed,

41
00:03:13,330 --> 00:03:16,870
the other code is ignored, so the code in here is ignored,

42
00:03:16,870 --> 00:03:22,690
if this condition is not met, then we'll ignore this code and instead the code in here will get executed

43
00:03:23,110 --> 00:03:26,170
and therefore for now, let's say we want to subtract,

44
00:03:26,170 --> 00:03:30,350
later I'll also cover the other cases but for now let's say I want to subtract here,

45
00:03:30,490 --> 00:03:35,780
so I will just move that subtraction code in here.

46
00:03:35,790 --> 00:03:42,630
Now you can also by the way write this like this for example, so move on onto a new line,

47
00:03:42,630 --> 00:03:45,430
if you only have one expression you want to execute,

48
00:03:45,480 --> 00:03:48,380
you could also omit the curly braces here,

49
00:03:48,390 --> 00:03:54,050
that would be allowed but for readability sake, I recommend that you always add curly braces

50
00:03:54,060 --> 00:03:59,990
even if you have only one expression in there and you also most often will find the style where the

51
00:03:59,990 --> 00:04:04,540
else statement or the else keyword then is on the same line as the closing curly brace.

52
00:04:04,560 --> 00:04:11,160
So whilst this would work, my code formatter even moves it back to this line if I autoformat the code

53
00:04:11,170 --> 00:04:16,450
because this is the most common way of writing this which you will find in most resources you find on

54
00:04:16,450 --> 00:04:19,890
the web and therefore this is the style I would recommend to you.

55
00:04:21,730 --> 00:04:24,940
We also use indentation here to improve readability,

56
00:04:24,940 --> 00:04:30,040
technically that's all not required, technically just as with the functions, you could write this all

57
00:04:30,040 --> 00:04:33,820
in one line like this but of course, that would be super hard to read

58
00:04:33,910 --> 00:04:37,000
which is why we split this across multiple lines.

59
00:04:37,300 --> 00:04:42,780
So now we're doing, we're performing different calculations based on the calculation type we're getting,

60
00:04:42,910 --> 00:04:51,710
now I also want to write my output here based on that and for that, let me actually check for add here,

61
00:04:51,710 --> 00:04:53,590
all capital case,

62
00:04:53,780 --> 00:04:56,510
so now calculation type could be forwarded here,

63
00:04:56,510 --> 00:05:01,490
now this all of a sudden is way more dynamic, it has no hardcoded values in there,

64
00:05:01,490 --> 00:05:05,680
instead this is now fully reusable because all these values are dynamic,

65
00:05:05,720 --> 00:05:09,670
we therefore get a different result by just tweaking these values

66
00:05:10,010 --> 00:05:17,240
and now here for the plus operator, we just have to introduce a new variable here, math operator

67
00:05:17,240 --> 00:05:20,010
for example which by default has no value

68
00:05:20,090 --> 00:05:26,450
but then here in the add case, I set this to be equal to a plus

69
00:05:26,470 --> 00:05:32,150
and now just with one equals sign because here I'm not checking two values, I'm not comparing two values,

70
00:05:32,150 --> 00:05:39,880
instead here I want to assign this as a value to the math operator variable and in the else case, math operator

71
00:05:39,890 --> 00:05:41,300
simply is a minus,

72
00:05:41,300 --> 00:05:47,780
so that's just a string of course and I'm using that because now I can use math operator down here and

73
00:05:47,780 --> 00:05:51,270
now these two lines are fully reusable,

74
00:05:51,320 --> 00:05:56,600
they have no hardcoded values in there and therefore they work for whatever the calculation we're

75
00:05:56,600 --> 00:05:57,410
performing,

76
00:05:57,530 --> 00:06:01,020
we just have to make sure we set these values correctly.

77
00:06:01,160 --> 00:06:06,320
Now calculation type should be received from outside and therefore what we can now do is here in the

78
00:06:06,380 --> 00:06:14,150
add case, we can remove all that code and instead call calculate result and pass in add as a calculation

79
00:06:14,150 --> 00:06:18,950
type and for subtract, we also remove all of that and I pass in

80
00:06:19,070 --> 00:06:26,360
subtract or for now actually, any other value than add will do the trick because if we pass in add, then

81
00:06:26,360 --> 00:06:31,910
this function will be executed and calculation type is set equal to add which means we make it into

82
00:06:31,910 --> 00:06:37,700
this block here, we make it into this block, this code gets executed and therefore we do the same which

83
00:06:37,700 --> 00:06:44,000
we previously did in here in add - we add the numbers, we set math operator to a plus and therefore down here,

84
00:06:44,000 --> 00:06:48,790
we forward add as a value for calculation type and a plus here for math

85
00:06:48,800 --> 00:06:54,860
operator. If anything else, then add is received, we currently always subtract the values here in the

86
00:06:54,860 --> 00:06:58,220
else block and math operator is set to a minus,

87
00:06:58,400 --> 00:07:05,150
so here we'll pass in whatever value calculation type held and the math operator will be a minus.

88
00:07:05,180 --> 00:07:12,140
So if we save that and we reload our calculator, you will notice that it works exactly as before for

89
00:07:12,140 --> 00:07:13,880
all these operations

90
00:07:13,880 --> 00:07:20,930
and most importantly also for plus and minus where we now reuse our code with help of this if else statement.
