1
00:00:01,980 --> 00:00:02,860
Welcome back.

2
00:00:03,240 --> 00:00:08,720
So in this lesson we get to cover one of the most important concepts in all of programming.

3
00:00:08,850 --> 00:00:14,520
And in fact I might argue that it's actually the most important concept which is conditionals.

4
00:00:14,520 --> 00:00:20,040
And the reason I say that conditionals are so important is that they are the one way that you add decisions

5
00:00:20,040 --> 00:00:21,240
to your code.

6
00:00:21,240 --> 00:00:28,800
It's how you add logic to your programming so give you a few examples of real world uses for conditionals

7
00:00:28,810 --> 00:00:29,100
.

8
00:00:29,370 --> 00:00:31,460
Let's take an example of user logon.

9
00:00:31,500 --> 00:00:37,230
So when you go to log in on a website you type a password in that form and there is code that checks

10
00:00:37,230 --> 00:00:40,690
that password that you typed in against the password in the database.

11
00:00:40,710 --> 00:00:45,480
And if they match you log into the site and you get redirected to another page.

12
00:00:45,600 --> 00:00:49,010
And if they don't match you usually get an error message or some sort.

13
00:00:49,170 --> 00:00:52,530
So there's two different paths given the same exact code.

14
00:00:52,530 --> 00:00:59,430
Another example would be when you buy something online when you type in your credit card information

15
00:00:59,430 --> 00:00:59,810
.

16
00:00:59,910 --> 00:01:04,830
If it works if the transaction goes through you might get a confirmation email you see a success screen

17
00:01:04,850 --> 00:01:05,040
.

18
00:01:05,250 --> 00:01:10,200
And if there's some problem with your information or you don't have funds in the account you're going

19
00:01:10,200 --> 00:01:11,320
to get an error message.

20
00:01:11,370 --> 00:01:13,390
Again two different outcomes.

21
00:01:13,410 --> 00:01:15,760
So now let's go see conditionals in action.

22
00:01:16,380 --> 00:01:22,970
So there are three key words that I need you to remember throughout this lecture which are if else if

23
00:01:23,580 --> 00:01:24,350
and else.

24
00:01:24,450 --> 00:01:30,990
So these are the three javascript conditional keywords and yes I know LCF is technically two English

25
00:01:30,990 --> 00:01:36,200
words but to javascript that is just one key word.

26
00:01:36,300 --> 00:01:42,060
So the example that we're going to use to illustrate conditionals is that of a bouncer at a concert

27
00:01:42,060 --> 00:01:43,710
venue.

28
00:01:43,800 --> 00:01:50,820
So the idea is that we're going to have an age variable and if that age if a person is younger than

29
00:01:50,820 --> 00:01:58,440
18 Unfortunately that person cannot get into our venue if that person is older than 18 but they're not

30
00:01:58,440 --> 00:01:59,740
yet 21.

31
00:01:59,910 --> 00:02:03,980
That means that they can enter but we have to draw an X in their arm or in their hand because they cannot

32
00:02:04,050 --> 00:02:04,700
drink.

33
00:02:04,980 --> 00:02:09,900
And then lastly anyone else if you're greater than 21 you can get in and drink.

34
00:02:09,900 --> 00:02:13,440
So those are the three things that we're going to try and write with code.

35
00:02:14,100 --> 00:02:19,390
So before we move on we're going to do a quick recap of the Boolean logic operators from the last video

36
00:02:19,400 --> 00:02:19,700
.

37
00:02:20,190 --> 00:02:24,540
And the reason that we're doing that is that we actually use our three keywords that I mentioned at

38
00:02:24,540 --> 00:02:31,080
the beginning if LCF and else in conjunction with these boolean operators to make our decisions to add

39
00:02:31,080 --> 00:02:32,340
the logic.

40
00:02:32,400 --> 00:02:38,090
So to recap we've got less than greater than less than or equal to greater than or equal to.

41
00:02:38,220 --> 00:02:39,330
Pop quiz here.

42
00:02:39,660 --> 00:02:41,580
What's the difference between these two.

43
00:02:41,640 --> 00:02:47,560
So Pozzi video and try and remind yourself the difference between chirpily calls and double equals.

44
00:02:48,600 --> 00:02:54,810
So the difference is that double equals performs type coercion while Tripoli equals as strict equals

45
00:02:54,800 --> 00:02:55,530
.

46
00:02:55,710 --> 00:02:56,680
So type coercion.

47
00:02:56,700 --> 00:02:57,770
Let's start there.

48
00:02:57,780 --> 00:03:03,580
Double equals means that the number one is actually going to be equal to the string one.

49
00:03:03,780 --> 00:03:07,920
When we used double equals because javascript does not care about type.

50
00:03:07,920 --> 00:03:13,960
However when we do triple equals which is strict It does care about type and value together.

51
00:03:14,040 --> 00:03:22,150
So one is triple equal to the number one vote one would not be triple equal to the string one.

52
00:03:22,620 --> 00:03:26,140
And then we've got these two operators and and or.

53
00:03:26,460 --> 00:03:32,270
And remember that and is only true if both sides are true however or is true.

54
00:03:32,310 --> 00:03:34,790
If one of the sides evaluates to TRUE.

55
00:03:35,580 --> 00:03:39,160
OK so now let's actually move on to writing our logic.

56
00:03:39,210 --> 00:03:45,240
So the first thing we're going to see is the if key word the if keyword takes a single expression instead

57
00:03:45,240 --> 00:03:48,840
of parentheses which it will evaluate to be true or false.

58
00:03:48,990 --> 00:03:54,960
And if it evaluates to be true whatever code we provide inside the curly braces in this case a console

59
00:03:55,110 --> 00:03:57,840
like statement will be executed.

60
00:03:57,840 --> 00:04:03,660
So this constant alt log will only be executed well it will only be printed out if age is less than

61
00:04:03,660 --> 00:04:04,370
18.

62
00:04:04,530 --> 00:04:10,300
If it is greater than 18 if it's equal to 18 we will not see this text printed up.

63
00:04:10,860 --> 00:04:16,750
So that's the first part that's our bouncer checking if you are too young to get it.

64
00:04:17,670 --> 00:04:20,210
So next up we're going to learn about else.

65
00:04:20,480 --> 00:04:24,020
And the way that LCF works is that it follows an if statement.

66
00:04:24,150 --> 00:04:26,970
It is a secondary condition.

67
00:04:26,970 --> 00:04:30,000
So in this case we're checking if age is less than 18.

68
00:04:30,000 --> 00:04:35,540
So if someone is too young to enter the venue if they're less than 18 they can't get in.

69
00:04:35,550 --> 00:04:40,990
The next thing to check only after we check the first one is are they older than 18.

70
00:04:41,040 --> 00:04:43,560
But less than 21.

71
00:04:43,770 --> 00:04:49,110
So LCF works the exact same way as if we pass an expression in parentheses that will evaluate to be

72
00:04:49,110 --> 00:04:51,460
true or false if it's true.

73
00:04:51,660 --> 00:04:57,570
The code inside the curly braces will be executed if it is not true if it's false nothing happens.

74
00:04:58,020 --> 00:05:00,760
So again LCF works just like if.

75
00:05:00,810 --> 00:05:04,190
Except that it must follow in their statement.

76
00:05:04,980 --> 00:05:06,750
So we have this line here.

77
00:05:06,780 --> 00:05:12,690
Age greater than 18 and age is less than 21 which is actually a little bit redundant.

78
00:05:13,080 --> 00:05:14,570
So think about this for a second.

79
00:05:14,580 --> 00:05:21,970
Plus a video and try to identify how we could refactor this.

80
00:05:22,050 --> 00:05:29,260
So this is a really minor change but you'll notice I got rid of if age is greater than 18 right here

81
00:05:29,270 --> 00:05:29,630
.

82
00:05:29,850 --> 00:05:35,720
And less than 21 and just turned it into if else LCF age is less than 21.

83
00:05:35,730 --> 00:05:41,800
So again just to reiterate we had this before and we refactored it into this.

84
00:05:41,880 --> 00:05:45,210
And the reason that we did it is it's redundant.

85
00:05:45,570 --> 00:05:53,150
The only way that this code is going to run is if this first if statement is false.

86
00:05:53,310 --> 00:06:02,160
So what that means is that this code here will only run if already we've established that age is not

87
00:06:02,160 --> 00:06:05,760
less than 18 or that is greater than or equal to 18.

88
00:06:05,790 --> 00:06:14,230
So if you are greater than or equal to 18 and less than 21 you can enter but you cannot drink.

89
00:06:14,340 --> 00:06:15,670
So the last step is the.

90
00:06:15,670 --> 00:06:17,510
Otherwise section which is.

91
00:06:17,510 --> 00:06:23,420
Else so else allows us to just have something else happen any other time.

92
00:06:23,490 --> 00:06:30,180
So in this case if you're not less than 18 and you're not between 18 and 21 that means that you can

93
00:06:30,180 --> 00:06:35,460
come on in and you can drink and so else does not take an expression in parentheses.

94
00:06:35,760 --> 00:06:40,560
It's different in that we just write else and then whatever we put in said it curly braces will only

95
00:06:40,560 --> 00:06:42,910
be run as a last ditch effort.

96
00:06:43,080 --> 00:06:49,080
So this will be run first and if nothing is printed out if this is not true then we'll move on to this

97
00:06:49,080 --> 00:06:49,300
.

98
00:06:49,440 --> 00:06:52,140
If this is not true then we'll move on to this.

99
00:06:52,410 --> 00:06:59,250
But remember if along the way either of these is true then everything stops and this else will not run

100
00:06:59,270 --> 00:06:59,420
.

101
00:06:59,550 --> 00:07:03,130
So if this is true this code is done.

102
00:07:03,930 --> 00:07:06,540
If this elf's if is false.

103
00:07:06,750 --> 00:07:10,320
But this is true or else never runs.

104
00:07:10,320 --> 00:07:15,440
So remember these are three different paths for one decision here.

105
00:07:15,550 --> 00:07:21,930
OK so your exercise here there's a few of them is to take the code that I just gave you with the bouncer

106
00:07:21,960 --> 00:07:23,100
for a concert venue.

107
00:07:23,220 --> 00:07:28,040
And to add a few let's say quirky other conditions.

108
00:07:28,080 --> 00:07:32,860
So if age is net negative we want to make sure that we print an error message.

109
00:07:33,780 --> 00:07:35,190
If age is 21.

110
00:07:35,190 --> 00:07:36,310
Exactly.

111
00:07:36,420 --> 00:07:39,880
We want to print happy 21st birthday give them a free shot.

112
00:07:40,410 --> 00:07:44,850
And if age is odd you should print your age is odd.

113
00:07:45,480 --> 00:07:48,360
So you have to do a little bit of math there to figure out if it's odd.

114
00:07:48,450 --> 00:07:53,760
And then the last one which is a bonus is to figure out if age is a perfect square and if you don't

115
00:07:53,760 --> 00:07:58,640
remember what a perfect square is you can look that up but it's basically a number whose square root

116
00:07:59,550 --> 00:08:01,830
the two factors are the exact same number.

117
00:08:01,860 --> 00:08:08,880
So four times for 16 so 16 is a perfect square four is a perfect square because two times two is four

118
00:08:08,880 --> 00:08:09,390
.

119
00:08:09,390 --> 00:08:11,280
All right so that's a super bonus.

120
00:08:11,280 --> 00:08:13,900
Have fun with this and I'll see in the next video.
