WEBVTT

1
00:00:01.270 --> 00:00:03.430
<v Instructor>Let's now return to our code</v>

2
00:00:03.430 --> 00:00:07.253
and learn how logical operators work in JavaScript.

3
00:00:09.270 --> 00:00:11.690
And let's actually use the Boolean variables

4
00:00:11.690 --> 00:00:13.310
from the last lecture.

5
00:00:13.310 --> 00:00:16.520
So the one about having a drivers license

6
00:00:16.520 --> 00:00:18.053
and having good vision.

7
00:00:20.460 --> 00:00:24.460
So, we are gonna create a variable for each of them.

8
00:00:24.460 --> 00:00:28.957
And the first one will be has drivers license

9
00:00:30.450 --> 00:00:33.440
and let's start by setting it to true.

10
00:00:33.440 --> 00:00:35.970
And remember that this one was variable A.

11
00:00:37.576 --> 00:00:41.283
And then let's create has good vision

12
00:00:44.190 --> 00:00:46.813
and also, let's initialize it with true.

13
00:00:48.250 --> 00:00:51.290
So these were the two A and B variables

14
00:00:51.290 --> 00:00:54.450
that we used in the last lecture as an example.

15
00:00:54.450 --> 00:00:57.263
And so it's a good idea to use these here as well.

16
00:00:58.650 --> 00:01:00.981
Okay, but now let's get started with the end

17
00:01:00.981 --> 00:01:03.530
and or operators.

18
00:01:03.530 --> 00:01:07.910
So let's log to the console, of using the end operator

19
00:01:07.910 --> 00:01:09.910
on these two variables.

20
00:01:09.910 --> 00:01:12.460
So has drivers license

21
00:01:12.460 --> 00:01:16.990
and now the end operator in JavaScript is this one.

22
00:01:16.990 --> 00:01:20.160
So it is we can combine two logical values.

23
00:01:20.160 --> 00:01:25.160
So has drivers license and has good vision.

24
00:01:26.570 --> 00:01:29.490
So let's see what the result will be.

25
00:01:29.490 --> 00:01:32.160
So this one is true, right?

26
00:01:32.160 --> 00:01:34.220
And this one is true as well.

27
00:01:34.220 --> 00:01:39.220
So the result of true and true, should be true, right?

28
00:01:41.200 --> 00:01:46.200
So let's see and yes, so that works.

29
00:01:47.010 --> 00:01:50.223
But now, of course, we will change one of them to false.

30
00:01:51.410 --> 00:01:56.410
So remember, the result of true and false will be false,

31
00:01:56.810 --> 00:01:59.870
because it's enough for one variable to be false

32
00:01:59.870 --> 00:02:03.100
for the result of the operation to be false.

33
00:02:03.100 --> 00:02:05.360
And so here, we can now confirm that

34
00:02:05.360 --> 00:02:07.990
and indeed, that is the result.

35
00:02:07.990 --> 00:02:10.450
But now let's see what it would look like

36
00:02:10.450 --> 00:02:12.670
with the OR operator.

37
00:02:12.670 --> 00:02:16.053
So I just copy this and paste it here.

38
00:02:17.070 --> 00:02:21.900
Now, instead of end we use OR and that looks like this.

39
00:02:21.900 --> 00:02:25.970
So again, true or false.

40
00:02:25.970 --> 00:02:27.620
What do you think it will be?

41
00:02:27.620 --> 00:02:32.400
Remember from the last lecture and the result is true.

42
00:02:32.400 --> 00:02:35.750
So it's enough for one of the variables to be true

43
00:02:35.750 --> 00:02:38.660
for the whole operation to become true.

44
00:02:38.660 --> 00:02:42.250
So I hope that's nothing new at this point.

45
00:02:42.250 --> 00:02:47.250
And of course, finally we can also use the NOT operator

46
00:02:47.490 --> 00:02:50.410
to basically invert one of the values.

47
00:02:50.410 --> 00:02:55.090
And the NOT operator in JavaScript is the exclamation mark.

48
00:02:55.090 --> 00:02:59.293
So not has driver's license will become,

49
00:03:00.670 --> 00:03:04.153
so it will change from true to false.

50
00:03:05.210 --> 00:03:09.280
All right, and now since we know how all of this works,

51
00:03:09.280 --> 00:03:12.490
we can use these variables to take a decision.

52
00:03:12.490 --> 00:03:14.660
So let's say that we want to determine

53
00:03:14.660 --> 00:03:17.370
whether Sarah should drive or not.

54
00:03:17.370 --> 00:03:20.960
So let's create a new Boolean variable and use the others

55
00:03:20.960 --> 00:03:22.183
to determine that.

56
00:03:24.620 --> 00:03:29.390
So let's say should drive

57
00:03:29.390 --> 00:03:32.070
and our model is that Sarah should drive,

58
00:03:32.070 --> 00:03:34.180
if she has a driver's license

59
00:03:34.180 --> 00:03:38.040
and at the same time has good vision, okay?

60
00:03:38.040 --> 00:03:40.570
And so as I mentioned in the last lecture,

61
00:03:40.570 --> 00:03:42.880
we can now use these Boolean variables,

62
00:03:42.880 --> 00:03:46.670
and this Boolean logic to model complex situations.

63
00:03:46.670 --> 00:03:49.140
And this one is actually pretty simple,

64
00:03:49.140 --> 00:03:52.750
but we will take it up a notch by the end of this lecture.

65
00:03:52.750 --> 00:03:57.430
So remember, model is that she has a driver's license,

66
00:03:57.430 --> 00:04:00.483
and that she has good vision.

67
00:04:01.540 --> 00:04:04.700
Okay, and now we can use this variable

68
00:04:04.700 --> 00:04:06.903
to finally take the decision.

69
00:04:08.010 --> 00:04:12.470
So let's then use this Boolean variable here and say,

70
00:04:12.470 --> 00:04:17.470
if she should drive, then lock to the console.

71
00:04:19.300 --> 00:04:22.750
Sarah is able to drive

72
00:04:24.130 --> 00:04:25.023
and if not,

73
00:04:26.460 --> 00:04:30.150
just log someone else

74
00:04:32.790 --> 00:04:34.123
should derive.

75
00:04:36.530 --> 00:04:40.510
Now, okay, and actually we can put the condition right here

76
00:04:40.510 --> 00:04:42.150
in the IF condition.

77
00:04:42.150 --> 00:04:44.020
So this was just to show you a little bit

78
00:04:44.020 --> 00:04:45.920
the thought process.

79
00:04:45.920 --> 00:04:47.300
But as I mentioned earlier,

80
00:04:47.300 --> 00:04:50.290
we usually create the condition right here

81
00:04:50.290 --> 00:04:52.980
in the IF statement.

82
00:04:52.980 --> 00:04:57.980
So give it a safe and now let's run it and the result is,

83
00:04:58.620 --> 00:05:01.010
someone else should drive.

84
00:05:01.010 --> 00:05:05.880
So that is because has good vision is false,

85
00:05:05.880 --> 00:05:08.720
and so our condition of has driver's license

86
00:05:08.720 --> 00:05:11.300
and has good vision is not true.

87
00:05:11.300 --> 00:05:15.140
Because it's only true in case that both of them are true,

88
00:05:15.140 --> 00:05:17.870
as you already know by now.

89
00:05:17.870 --> 00:05:20.140
So this one is false right now.

90
00:05:20.140 --> 00:05:23.770
And so therefore the else block will be executed.

91
00:05:23.770 --> 00:05:26.363
But of course, we can change it once more.

92
00:05:27.550 --> 00:05:30.570
And so right now, both of them are true.

93
00:05:30.570 --> 00:05:35.310
So has driver's license true and has good vision true.

94
00:05:35.310 --> 00:05:40.263
So true and true, is true and so Sarah is able to drive.

95
00:05:41.530 --> 00:05:45.970
Let's check that and indeed, great.

96
00:05:45.970 --> 00:05:48.510
And but this one working now perfectly,

97
00:05:48.510 --> 00:05:50.610
let's take it to the next level

98
00:05:50.610 --> 00:05:53.173
and add another Boolean variable here.

99
00:05:54.140 --> 00:05:59.133
So, let's now also add is tired.

100
00:06:01.370 --> 00:06:03.693
And let's set it to true as well.

101
00:06:04.580 --> 00:06:07.363
So we can say that this is our variable C.

102
00:06:08.340 --> 00:06:13.130
So to use more than two variables with, for example,

103
00:06:13.130 --> 00:06:16.763
the end operator, let's just go ahead and copy this one.

104
00:06:17.890 --> 00:06:20.470
So if you want to say has driver's license

105
00:06:20.470 --> 00:06:23.600
or has good vision or is tired.

106
00:06:23.600 --> 00:06:27.730
All you have to do is to add another OR operator here,

107
00:06:27.730 --> 00:06:29.730
just like you would add another plus

108
00:06:29.730 --> 00:06:31.563
or another minus operator.

109
00:06:33.270 --> 00:06:36.760
And the result of this should of course, be true,

110
00:06:36.760 --> 00:06:39.143
because while all of them are true.

111
00:06:40.810 --> 00:06:42.323
And indeed, it is.

112
00:06:43.170 --> 00:06:45.373
And if we set them all to end,

113
00:06:48.220 --> 00:06:53.037
then it is also true, because all of them are true.

114
00:06:53.037 --> 00:06:55.430
And if we set this one to false,

115
00:06:55.430 --> 00:07:00.320
then as you can guess the result will be false.

116
00:07:00.320 --> 00:07:03.100
And that is because as I explained in the last lecture,

117
00:07:03.100 --> 00:07:06.230
it's enough for one of the operands to be false

118
00:07:06.230 --> 00:07:09.673
to make the whole end operation false as well.

119
00:07:12.420 --> 00:07:16.170
Okay, but let it set back to true.

120
00:07:16.170 --> 00:07:19.990
And now, we can use this to improve our decision making,

121
00:07:19.990 --> 00:07:23.670
whether Sarah should drive or not.

122
00:07:23.670 --> 00:07:25.900
So with this third variable now,

123
00:07:25.900 --> 00:07:27.910
we want Sarah to be able to drive

124
00:07:27.910 --> 00:07:31.970
if she has a driver's license, if she has good vision

125
00:07:31.970 --> 00:07:35.460
and if she is not tired, right?

126
00:07:35.460 --> 00:07:39.010
So let's now translate that into a condition.

127
00:07:39.010 --> 00:07:40.770
And I will just copy this one

128
00:07:40.770 --> 00:07:43.820
and then comment all of it out using Command

129
00:07:43.820 --> 00:07:47.460
or Control/ just like many times before

130
00:07:48.650 --> 00:07:52.030
and now we will improve this one.

131
00:07:52.030 --> 00:07:54.500
So this one, of course, is the same

132
00:07:54.500 --> 00:07:58.180
that she needs to have a driver's license and good vision.

133
00:07:58.180 --> 00:08:02.310
But now additionally, we want her to be not tired.

134
00:08:02.310 --> 00:08:05.160
So right now she is tired, it's true

135
00:08:05.160 --> 00:08:08.460
and that's, of course, not good for driving.

136
00:08:08.460 --> 00:08:11.550
So we want the opposite of being tired.

137
00:08:11.550 --> 00:08:15.380
And so therefore, we need to invert it.

138
00:08:15.380 --> 00:08:17.080
So do you remember how that works?

139
00:08:18.360 --> 00:08:21.760
All we need to do is to use the NOT operator.

140
00:08:21.760 --> 00:08:26.520
So not is tired, all right?

141
00:08:26.520 --> 00:08:28.930
So let's read this again,

142
00:08:28.930 --> 00:08:32.770
Sarah is able to drive if she has a driver's license.

143
00:08:32.770 --> 00:08:36.290
So if this one is true, if she has good vision,

144
00:08:36.290 --> 00:08:40.120
so if this one is true and if she is not tired,

145
00:08:40.120 --> 00:08:42.313
so if all of this is true here.

146
00:08:43.420 --> 00:08:46.810
So only in this condition, Sarah is able to drive.

147
00:08:46.810 --> 00:08:50.010
Now how is this part here true?

148
00:08:50.010 --> 00:08:54.890
Well, it's true, when is tired is false, right?

149
00:08:54.890 --> 00:08:56.310
So what do you think will happen

150
00:08:56.310 --> 00:08:58.960
in this situation right now?

151
00:08:58.960 --> 00:09:01.353
Well, let's test and then analyze.

152
00:09:02.350 --> 00:09:05.790
So we get the result, someone else should drive.

153
00:09:05.790 --> 00:09:07.590
So why is that?

154
00:09:07.590 --> 00:09:09.030
Well, this one is true,

155
00:09:09.030 --> 00:09:11.750
this one is true as we see up here,

156
00:09:11.750 --> 00:09:14.880
and then is tired is also true.

157
00:09:14.880 --> 00:09:18.500
However, not is tired is false.

158
00:09:18.500 --> 00:09:20.450
So this part here is false.

159
00:09:20.450 --> 00:09:25.000
And so true and true and false is of course, false.

160
00:09:25.000 --> 00:09:27.870
And so therefore, we get the else block,

161
00:09:27.870 --> 00:09:29.860
someone else should drive.

162
00:09:29.860 --> 00:09:30.893
Now to fix this,

163
00:09:31.780 --> 00:09:36.780
we need to change is tired to false, all right?

164
00:09:37.330 --> 00:09:40.750
And so right now is tired is false

165
00:09:40.750 --> 00:09:43.850
and therefore not is tired will become true.

166
00:09:43.850 --> 00:09:47.090
And then we have true and true and true

167
00:09:47.090 --> 00:09:50.743
and then in this situation, Sarah will be able to drive.

168
00:09:51.940 --> 00:09:54.870
Yes, indeed, it works now.

169
00:09:54.870 --> 00:09:58.340
So as you see, with these three Boolean operators,

170
00:09:58.340 --> 00:10:02.410
we can model all kinds of companies decisions like this one.

171
00:10:02.410 --> 00:10:06.090
Now, I know that this was probably a bit confusing,

172
00:10:06.090 --> 00:10:07.890
but please don't worry too much,

173
00:10:07.890 --> 00:10:10.970
because it's all very confusing for everyone

174
00:10:10.970 --> 00:10:14.350
when they first learn how to program, okay?

175
00:10:14.350 --> 00:10:16.710
Just take some time after this video

176
00:10:16.710 --> 00:10:19.800
and analyze again exactly what happens here.

177
00:10:19.800 --> 00:10:22.190
Maybe also together with the last video

178
00:10:22.190 --> 00:10:23.970
to really make sure that you understand

179
00:10:23.970 --> 00:10:25.890
what is going on here.

180
00:10:25.890 --> 00:10:28.860
But of course, the best way to retain any knowledge

181
00:10:28.860 --> 00:10:31.840
is by practicing and reinforcing it.

182
00:10:31.840 --> 00:10:35.363
And so let's do that now in the next coding challenge.

